How do I replace single quotes with another charac

2020-02-05 01:54发布

问题:

I have a flat file where I have multiple occurrences of strings that contains single quote, e.g. hari's and leader's.

I want to replace all occurrences of the single quote with space, i.e.

  • all occurences of hari's to hari s
  • all occurences of leader's to leader s

I tried

sed -e 's/"'"/ /g' myfile.txt

and

sed -e 's/"'"/" "/g' myfile.txt

but they are not giving me the expected result.

回答1:

Try to keep sed commands simple as much as possible. Otherwise you'll get confused of what you'd written reading it later.

#!/bin/bash
sed "s/'/ /g" myfile.txt


回答2:

This will do what you want to

echo "hari's"| sed 's/\x27/ /g'

It will replace single quotes present anywhere in your file/text. Even if they are used for quoting they will be replaced with spaces. In that case(remove the quotes within a word not at word boundary) you can use the following:

echo "hari's"| sed -re 's/(\<.+)\x27(.+\>)/\1 \2/g'

HTH



回答3:

Just go leave the single quote and put an escaped single quote:

sed 's/'\''/ /g' input

also possible with a variable:

quote=\'
sed "s/$quote/ /g" input


回答4:

Here is based on my own experience.

Please notice on how I use special char ' vs " after sed

This won't do (no output)

2521 #> echo 1'2'3'4'5 | sed 's/'/ /g'
>
>
>

but This would do

2520 #> echo 1'2'3'4'5 | sed "s/'/ /g"
12345


回答5:

The -i should replace it in the file sed -i 's/“/"/g' filename.txt

if you want backups you can do sed -i.bak 's/“/"/g' filename.txt



回答6:

I had to replace "0x" string with "32'h" and resolved with:

sed 's/ 0x/ 32\x27h/'