How do I replace single quotes with another charac

2020-02-05 01:25发布

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.

6条回答
乱世女痞
2楼-- · 2020-02-05 01:45

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
查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-02-05 01:48

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
查看更多
Deceive 欺骗
4楼-- · 2020-02-05 01:54

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

查看更多
forever°为你锁心
5楼-- · 2020-02-05 01:58

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

sed 's/ 0x/ 32\x27h/'
查看更多
等我变得足够好
6楼-- · 2020-02-05 02:04

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

查看更多
萌系小妹纸
7楼-- · 2020-02-05 02:07

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
查看更多
登录 后发表回答