Replace String using Sed? - linux

I am new to Unix and Linux and am trying to replace a certain strings in a file using sed.
This is what I have so far:
Param1=CHANGEME
Param2=Value2
Param3=CHANGEME
Param4=Value4
If I do this command :- sed -i 's/CHANGEME/VALUE/g' input.txt, "CHANGEME" will be replaced by the text "VALUE" which is fine.
What if I want to change "Value" with it's corresponding value number so essentially for Param1=Value1 and for Param3=Value3 ?
How can I achieve that? Thank you

Using GNU sed ...
Your snippet saved to hugger:
cat hugger
Param1=CHANGEME
Param2=value2
Param3=CHANGEME
Param4=value
The following sed command does what you want, I think:
sed -r '/CHANGEME/s/Param([0-9]+)=.*/Param\1=Value\1/' hugger
Param1=Value1
Param2=value2
Param3=Value3
Param4=value4

Related

How to replace the value after = using sed command to null

I have scenario where i want to replace the digit or string after equal to = to null [ blank ]
Without creating new file need to replace in the same file
Data in file :
name=vilas
age=21
code=1345
Need to replace the digit after code=1345 to code=
I have tried this but stuck
sed -i 's/^code=$/code=/1g' file.txt
Note : The value after code= is going to be dynamic need to use regex pattern match which i am not good with
var1=456
$ sed -Ei "s/(code=).*/\1$var1/" file
name=vilas
age=21
code=456
Based on your question:
On MacOS and FreeBSD
sed -i '' -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
On CentOS and Debian
sed -i -E 's/^code=[[:digit:]]+$/code=/g' test1.txt
You can update the character class to fit your needs.
where
-i Edit files in-place
-E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's).
s The substitute command
g Apply the replacement to all matches to the regexp, not just the first.
GNU docs: https://www.gnu.org/software/sed/manual/html_node/Character-Classes-and-Bracket-Expressions.html
SED docs: https://www.gnu.org/software/sed/manual/sed.html

How do you change column name in csv file with linux on Mac

Simply just want to change few column names in a csv file and store it as it is using this code:
sed -i -e "1s/oldcolname/" -e "1s/newcolname/" xxx.csv.
But it does not work. I got the error message :
sed: 1: "1s/oldcolname/":unterminated substitute in regular expression.
Anyone knows how to rewrite it? Thanks!
If you want to simply replace oldcolname with newcolname, here's a quick answer:
sed -i -e 's/oldcolname/newcolname/' your_file.csv

sed removes one string in one file

I'm trying to remove one string in one file by using:
sed -i -e '/example/' test.txt
But I've got the following error:
sed: -e expression #1, char 6: missing command
What is it missing and why?
Thanks!
/example/ is an address which tells sed where to run commands - on a line containing the string example. You didn't specify any commands.
This is a commnand that replaces the string example with an empty string:
's/example//'
try:
sed '/example/d' test.txt
Explanation as suggested by #Nathan Tuggy
sed will search for the given string and will 'D'elete it
user#host:~$ cat test.txt
one
two
three
user#host:~$ sed '/two/d' test.txt
one
three

Sed replace date

So, I have looked over other questions and nothing specific seems like it can help me. I have a file that has a date variable set like mm/dd/yyyy that I would like to replace the date with.
For example:
version.js
//Application Version Information
app_date="7/07/2015";
...
And I would like to use sed to replace it. I actually have a shell script that replaces many things in this file, and all work except for the date. Currently what I'm trying is:
sed -i -e 's:app_date="[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'
But this isn't getting me the results I want. I've also tried:
sed -ibak 's/app_date=\"[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]\"/app_date=\$(date +%m/%d/$Y)\"/g'
Neither seem to work.
Any ideas?
Edited to add:
The solution I've successfully employed is Sean Bright's with double quotes:
sed -i -e "s:app_date=".*";:app_date="$(date +%m/%d/%Y)";:g" version.js
This works perfectly for my needs.
I bet your script would work if the date you were matching was October 10th...
sed -i -e 's:app_date="[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'
Add the *s after your day and month expressions as above. [0-9][0-9] will not match 7.
Now that I think about it, because you don't care about the date that is already in the file, most of this is unnecessary, you can simply do:
sed -i -e 's:app_date=".*";:app_date="'$(date +%m/%d/%Y)'";:g' version.js
You're using the wrong shell quotes. the command substitution $() will not be expanded inside single quotes.
sed -i "/^app_date=/capp_date=\"$(date +%m/%d/%Y)\";" version.js # GNU only
Another approach, using a file editor instead of a stream editor:
ed -s version.js << EOF
/^app_date=/c
app_date="$(date +%m/%d/%Y)";
.
w
EOF
For replacing date with anything in Linux
n=`date +%d`
cal>temp
If [$n -lt 10]
//10 is just a predction for condition
then
Sed s/"$n"/*/g temp
else
Sed s/"$n"/**/g temp
fi
// s is text substituting option
//* is anything that replaces the date
// g is global replacement

Linux Shell Programming. Implementing a Search, Find and Replace Technique

I have to implement an application in shell programming (Unix/Linux).
I have to search a word from a text file and replace that word with my given word. I have a knowledge on shell and still learning.
I am not expecting source code. Can anybody help me or suggest me or give me some similar solution....
cat abc.txt | grep "pattern" | sed 's/"pattern"/"new pattern"/g'
The above command should work
Thanks,
Regards,
Dheeraj Rampally
Say you are looking for pattern in a file (input.txt) and want to replace it with "new pattern" in another (output.txt)
Here is the main idea, without UUOC:
<input.txt sed 's/"pattern"/"new pattern"/g' >output.txt
todo
Now you need to embed this line in your program. You may want to make it interactive, or a command that you could use with 3 parameters.
edit
I tried to avoid the use of output.txt as a temporary file with this:
<input.txt sed 's/"pattern"/"new pattern"/g' >input.txt
but it empties input.txt for a reason I can't understand. So I tried with a subshell, so:
echo $(<input.txt sed 's/pattern/"new pattern"/g')>input.txt
... but the echo command removes line breaks... still looking.
edit2
From https://unix.stackexchange.com/questions/11067/is-there-a-way-to-modify-a-file-in-place , it looks like writing to the very same file at once it not easy at all. However, I could do what I wanted with sed -i for linux only:
sed -i 's/pattern/"new pattern"/g' input.txt
From sed -i + what the same option in SOLARIS , it looks like there's no alternative, and you must use a temporary file:
sed 's/pattern/"new pattern"/g' input.txt > input.tmp && mv input.tmp input.txt

Resources