如何用sed中的另一个字符替换单引号?

时间:2020-12-28 19:21:55

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
  • 所有hari都出现在哈里
  • 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.

但他们没有给我预期的结果。

5 个解决方案

#1


34  

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

尝试尽可能简单地保持sed命令,否则你会在以后阅读之后对你所写的内容感到困惑。

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

#2


18  

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

HTH

#3


10  

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


0  

Here is based on my own experience.

这是基于我自己的经验。

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

请注意我在sed之后如何使用特殊字符'vs'

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


0  

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

-i应该在文件sed -i's /“/”/ g'filename.txt中替换它

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

如果你想要备份你可以做sed -i.bak的/“/”/ g'filename.txt

#1


34  

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

尝试尽可能简单地保持sed命令,否则你会在以后阅读之后对你所写的内容感到困惑。

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

#2


18  

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

HTH

#3


10  

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


0  

Here is based on my own experience.

这是基于我自己的经验。

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

请注意我在sed之后如何使用特殊字符'vs'

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


0  

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

-i应该在文件sed -i's /“/”/ g'filename.txt中替换它

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

如果你想要备份你可以做sed -i.bak的/“/”/ g'filename.txt