Remove all special characters from a file. - unix

I have a file with special characters and I only want alpha/numbers back.
File:
!12asdfasdf#$!#$%$##123123%##$%123
Return:
12asdfasdf123123123

You could either use sed or awk for this:
Sed
sed "s/[^a-z|0-9]//g;" file
Awk
awk '{gsub(/[[:punct:]]/,"")}1' file

Related

Replace Text using variables in Sed not working

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

Replacing text in sed with escape characters

I have a very large file where I need to replace the characters \x01\n with the character \n. How would this be done with sed? So far I have:
$ sed -i 's/\x01\n/\n' file
extra characters at the end of d command
But perhaps I'm missing a few escape characters.
Don't write \n in the line oriented sed:
sed -i 's/\x01$//' file
I think you are just missing the closing '/':
$ sed -i 's/\x01\n/\n/' file
This might work for you (GNU sed):
sed -zi 's/\x01\n/\n/g' file
This slurps in the whole (or parts delimited by \x00) file and replaces \x01\n by \n globally.

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.

Replacing all commas with spaces - S/R with a Unix Command?

I'm looking for a Unix command that will allow me to search/replace in a file - I need to replace all commas in a certain file with spaces. I need to do this in a script and I'm looking to avoid parsing/reading the file line by line. Is there a simple unix command that will allow me to do this?
sed 's/,/ /g' filename >resultfile
You can use awk, sed, vi, ex or even Perl, PHP etc ... depends what you are proficient with.
sed example:
sed -i 's/,/ /g' filename_here

How do I replace a token with the result of `pwd` in sed?

I'm trying to do something like this:
sed 's/#REPLACE-WITH-PATH/'`pwd`'/'
Unfortunately, I that errors out:
sed: -e expression #1, char 23: unknown option to `s'
Why does this happen?
You need to use a different character instead of /, eg.:
sed 's?#REPLACE-WITH-PATH?'`pwd`'?'
because / appears in the pwd output.
in sed, you can't use / directly, you must use '/'.
#!/bin/bash
dir=$`pwd`/
ls -1 | sed "s/^/${dir//\//\\/}/g"
sed 's:#REPLACE-WITH-PATH:'`pwd`':' config.ini
The problem is one of escaping the output of pwd correctly. Fortunately, as in vim, sed supports using a different delimiter character. In this case, using the colon instead of slash as a delimiter avoids the escaping problem.
instead of fumbling around with quotes like that, you can do it like this
#!/bin/bash
p=`pwd`
# pass the variable p to awk
awk -v p="$p" '$0~p{ gsub("REPLACE-WITH-PATH",p) }1' file >temp
mv temp file
or just bash
p=`pwd`
while read line
do
line=${line/REPLACE-WITH-PATH/$p}
echo $line
done < file > temp
mv temp file

Resources