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
Related
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
I'm producing many files with numeric fields. All the fields end with . Like 1234.
I need to Replace all occurrences of '.,' with a ','in all files
Assuming all the files are in the same dir, AND you're using an new-ish gnu sed that supports the -i (inplace) optin , you can do
cd /path/to/dataDir
for file in * ; do
sed -i 's/\([0-9]\)\.,/\1./g' "$f"
done
If you're using Mac OSX, you can either supply a file extension to the -i option like
sed -i".bak" ....
or indicate "overwrite existing" with
sed -i""
If you're in an vendored-unix environment, you may need to manage the output yourself. Then you can replace inner-loop with
sed s/\([0-9]\)\.,/\1./g' "$f" > "$f".new && /bin/mv "$f".new "$f"
IHTH
I want to empty a file using sed command. I searched lot of forums and tutorial. There is no available to delete all contents of the file. How to delete all contents of the file using sed command.
It looks like a strange request. Anyway, this is a way:
sed -i '/^/d' file
sed -i does an in-place replacement.
/^/ matches lines, in this case all of them because ^ means "beginning of line".
/d deletes them.
Or shorter (thanks glenn jackman as always):
sed -i d file
You don't need sed for this. To empty a file:
> filename
with no command, that redirection will truncate the file.
Try this sed. It will remove all.
sed -ni '' file
n do not print if not told to do so.
i in place.
Since no code is given, file will be replaced by nothing.
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.
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