Replace Text using variables in Sed not working - unix

I need to modify an xml file using Sed to replace the line
url="jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net"
with
url="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
I have stored the lines like this
ACTUAL_DB=$(sed -n 's#^.*url="\(.*\).*"#\1#p' $FILE.xml)
and
NEW_DB="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
And the replacing method is this one
sed -i "s#$ACTUAL_DB#$NEW_DB#g" $File.xml
The problem is that when I run the script the file stays the same.
I have echoed the variables and all of them return the correct values.

Assuming the file you have is File.xml (if it is not a variable), you may use
sed -i "s#${ACTUAL_DB}#${NEW_DB}#g" File.xml
Try also with other delimiters:
sed -i "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml
If your sed does not support -i use
sed "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml 1<> File.xml
See sed edit file in place

So I saved the output into another file and found out that the string had an extra space
so it looked like this
ACTUAL_DB= "jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net "
I removed the extra space with
"$(echo -e "${ACTUAL_DB}" | tr -d '[:space:]')"
And now the sed is working as intended

Related

using sed to replace with wildcards

I am trying to replace this text (whatever the xxxx) :
{"type":"record","name":"xxxx","namespace":"example","fields":[{"name":"name","type":"string"}, {"name":"favorite_color","type":["string","null"]}]}
with this one:
{"type":"record","name":"yyyy","namespace":"example","fields":[{"name":"name","type":"string"},{"name":"favorite_color","type":["string","null"]}]}
I am using sed, but with this command:
sed 's/\"name\"\:\".*\"/\"name\"\:\"yyyy\"/' file.json
I am getting:
{"type":"record", "name":"yyyy"]}]}
because it replaces all the content until the last double quote.
How could I use the wildcards to replace only the text until the next double quote?
You can try with this:
sed 's/\"name\":\"[^\"]*\"/\"name\":\"yyyy\"/' file.json
Hence, modify the regular pattern so it looks until the next double quotes instead of any character.
If you want to write the changes to the file, you'll need -i flag, in addition to -r flag to use regular expressions. And if you want this to be applied to multiple occurrences, you can add a g to the sed command:
sed -i -r 's/\"name\":\"[^\"]*\"/\"name\":\"yyyy\"/g' file.json
Update: not needed to escape double quotes... The command could be just this:
sed -i -r 's/"name":"[^"]*"/"name":"yyyy"/g' file.json

Command to find a string and delete it from a file

I have a property file mail.properties. The content of the file is
ErrorEmailTo.server1=abc_1#gmail.com,bcd_1#gmail.com,efg_1#gmail.com
ErrorEmailFrom.server1=abc_1#gmail.com,bcd_1#gmail.com,efg_1#gmail.com
ErrorEmailCC.server1=abc_1#gmail.com,efg_1#gmail.com,bcd_1#gmail.com
...and numerous such entries.
I want to find bcd_1#gmail.com and delete it wherever it is present. The output property file (mail.properties) should look like :
ErrorEmailTo.server1=abc_1#gmail.com,efg_1#gmail.com
ErrorEmailFrom.server1=abc_1#gmail.com,efg_1#gmail.com
ErrorEmailCC.server1=abc_1#gmail.com,efg_1#gmail.com
You can try this sed,
sed 's/bcd_1#gmail.com,\?//g; s/,*$//' mail.properties
Use -i option for in-place edit.
sed -i 's/bcd_1#gmail.com,\?//g; s/,*$//' mail.properties
sed -e 's/bcd_1#gmail\.com//g' -e 's/,,/,/g' -e 's/,$//g' -e 's/=,/=/g' mail.properties > mail.properties_new

search and replace the value inside tags using script

I have a file like this. abc.txt
<ra><r>12.34</r><e>235</e><a>34.908</a><r>23</r><a>234.09</a><p>234</p><a>23</a></ra>
<hello>sadfaf</hello>
<hi>hiisadf</hi>
<ra><s>asdf</s><qw>345</qw><a>345</a><po>234</po><a>345</a></ra>
What I have to do is I have to find <ra> tag and for inside <ra> tag there is <a> tag whose valeus I have to replace by 0.00.
grep "<ra>" "abc.txt" | grep "<a>"
I am able to find and but I dont know how to change.
Output file for this:-
<ra><r>12.34</r><e>235</e><a>0.00</a><r>23</r><a>0.00</a><p>234</p><a>0.00</a></ra>
<hello>sadfaf</hello>
<hi>hiisadf</hi>
<ra><s>asdf</s><qw>345</qw><a>0.00</a><po>234</po><a>0.00</a></ra>
Replace using awk and gsub
awk '/^<ra>/ {gsub(/<a>[^<]*</,"<a>0.00<")}1' file
<ra><r>12.34</r><e>235</e><a>0.00</a><r>23</r><a>0.00</a><p>234</p><a>0.00</a></ra>
<hello>sadfaf</hello>
<hi>hiisadf</hi>
<ra><s>asdf</s><qw>345</qw><a>0.00</a><po>234</po><a>0.00</a></ra>
This sed should work:
sed -i.bak '/<ra>/s~\(<a>\)[^<]*\(</a>\)~\10.00\2~g' abc.txt
<ra><r>12.34</r><e>235</e><a>0.00</a><r>23</r><a>0.00</a><p>234</p><a>0.00</a></ra>
<hello>sadfaf</hello>
<hi>hiisadf</hi>
<ra><s>asdf</s><qw>345</qw><a>0.00</a><po>234</po><a>0.00</a></ra>
Because of -i (inline) switch this sed will save the changes in original file itself.
You can try with the following code:
$ sed -e '/<ra>/ s#<a>[^<]*<#<a>0.00<#g' file
<ra><r>12.34</r><e>235</e><a>0.00</a><r>23</r><a>0.00</a><p>234</p><a>0.00</a></ra>
<hello>sadfaf</hello>
<hi>hiisadf</hi>
<ra><s>asdf</s><qw>345</qw><a>0.00</a><po>234</po><a>0.00</a></ra>
It is based on this structure:
Print # in lines starting with BBB just if there was not ^# before
sed -e '/^BBB/ s/^#*/#/' -i file
changing the delimiter to a # so we do not need to escape the / in </a>.
Note that if you want the file to be updated you need to add -i to the sed (sed -i -e ...). Otherwise the result will be printed in the stdout.

What does sed -i option do?

I'm debugging a shell script and trying to find out the task performed by the following command:
sed -i '1,+999d' /home/org_user/data.txt
I need to change this command as its failing with the following error:
illegal option sed -i
But before changing, I need to understand the BAU functioning.
Appreciate any inputs in this regard.
An applicable use of this is as follows. Say you have the following file file.txt:
1, 2, 6, 7, "p"
We want to replace "p" with 0.
sed 's/"p"/0/g' file.txt
Using the above simply prints the output into command line.
You'd think why not just redirect that text back into the file like this:
sed 's/"p"/0/g' file.txt > file.txt
Unfortunately because of the nature of redirects the above will simply produce a blank file.
Instead a temp file must be created for the output which later overwrites the original file something like this:
sed 's/"p"/0/g' file.txt > tmp.txt && mv tmp.txt file.txt
Instead of doing the long workaround above sed edit in place option with -i allows for a much simpler command:
sed -i 's/"p"/0/g' file.txt
If -i option given, sed edit files in place.
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
from sed(1)
http://www.gnu.org/software/sed/manual/sed.html#Introduction
Some implementations of sed do not support the -i option. What it does can be simulated by
sed -e '...' file > tmp
mv tmp file

How to make sed read its script from a file?

Recently I came across following grep command:
/usr/xpg4/bin/grep -Ff grep.txt input.txt > output.txt
which as per my understanding means that from input.txt, grep the matter contained in grep.txt and output it to output.txt.
I want to do something similar for sed i.e. I want to keep the sed commands in a separate file (say sed.txt) and want to apply them on input file (say input.txt) and create a output file (say output.txt).
I tried following:
/usr/xpg4/bin/sed -f sed.txt input.txt > output.txt
It does not work and I get the following error:
sed: command garbled
The contents of files mentioned above are as below:
sed.txt
sed s/234/acn/ input.txt
sed s/78gt/hit/ input.txt
input.txt
234GH
5234BTW
89er
678tfg
234
234YT
tfg456
wert
78gt
gh23444
Your sed.txt should only contain sed commands: No prefixing with sed or suffixing with an input file. In your case it should probably be:
# sed.txt
s/234/acn/
s/78gt/hit/
When ran on your input:
$ /usr/xpg4/bin/sed -f sed.txt input.txt
acnGH
5acnBTW
89er
678tfg
acn
acnYT
tfg456
wert
hit
ghacn44
Rather than keeping the sed commands in a separate text file, you may want to try creating a sed script. The file below can run directly on your data files:
./myscript.sed inputfile.txt > outputfile.txt
#!/bin/sed -f
s/234/acn/
s/78gt/hit/

Resources