Sed to simultaneously remove single and double quotes

I am trying to remove single quotes and double quotes from a file. Can I do this in one sed command?

I'm trying to:

sed 's/\"//g;s/\'//g' txt file 

but get this error

`` '' has no equal.

Please, help.

+7
sed
source share
5 answers

Another possibility would be to use tr :

 tr -d \'\" file 
+12
source share

Try instead:

 sed -e 's|["'\'']||g' txt 
+5
source share

You cannot escape a single quote within a pair of single quotes in a shell. Although double quotes can be avoided. After the sed command should work:

 sed "s/['\"]//g" file 
+3
source share

You can use the commands below

 sed "s/'/ /g" file.txt > newfile.txt sed 's/\"//g' newfile.txt > Required_file.txt 

Required_file.txt - the final result.

+2
source share

Well, that's what I came to.

First, I learned with ord () what codes are for single and double quotes, and then I used the $ (..) syntax to pass it into an unquoted sed expression. I used XX and yy instead of empty lines. Obviously, this is faaar from the optimal, i.e. They may need to be combined into a single expression, I recommend that you experiment with it. There are several methods to avoid quoting issues, you can also put the sed expression in a separate file so that it is not interpreted by the shell. The ord () / chr () character is also useful when you are trying to deal with single unreadable characters in the output, for example. UTF strings on a console other than UTF.

 dtpwmbp:~ pwadas$ echo '"' | perl -pe 'print ord($_) . "\n";' 34 " dtpwmbp:~ pwadas$ echo "'" | perl -pe 'print ord($_) . "\n";' 39 ' dtpwmbp:~ pwadas$ echo \'\" '" dtpwmbp:~ pwadas$ echo \'\" | sed -es/$(perl -e 'print chr(34) . "\n"')/XX/g | sed -es/$(perl -e 'print chr(39) . "\n"')/yy/g yyXX dtpwmbp:~ pwadas$ 

EDIT (note that this time both characters are replaced with the same string "yy"). There may be some shell utilities for โ€œtranslatingโ€ characters into character codes and vice versa, i.e. this should be possible without using perl or another language translator.

 dtpwmbp:~ pwadas$ echo \'\" | sed -es/[`perl -e 'print chr(34) . chr(39)'`]/yy/g yyyy dtpwmbp:~ pwadas$ 

and here is another way in the shell, perhaps even simpler

 dtpwmbp:~ pwadas$ X="'"; Y='"' ; echo $X$Y; echo $X$Y | sed -e "s/$X/aa/g;s/$Y/bb/g" '" aabb dtpwmbp:~ pwadas$ 
0
source share

All Articles