【发布时间】:2019-10-29 23:31:11
【问题描述】:
我试图在 Unix 的文件中只用双引号 (") 替换单引号 (') 而不是撇号 (')。撇号 (') 必须保持原样。
我通过按顺序执行 3 个 sed 命令获得了所需的输出。但是,我无法解决最后一行的“假新闻”。
sed -i 's/'\''/"/g' test.txt
sed -i 's/"s/'\''s/g' test.txt
sed -i 's/s"/s'\''/g' test.txt
第一个 sed - 将所有单引号转换为双引号。
2nd sed - 将所有双引号(后跟 s)转换为单引号。
3rd sed - 将所有后跟双引号的 s 转换为单引号。
输入文件-
Hello Sir!
How are you?
How's your health?
All 'good'?
Charles' here.
'fake news'
预期输出-
Hello Sir!
How are you?
How's your health?
All "good"?
Charles' here.
"fake news"
【问题讨论】:
-
如何处理
'fake news'? -
@choroba 是的,我编写的 3 sed 命令将无法处理“假新闻”。我需要解决这个问题。
-
对于任何解决方案,请考虑它如何处理像
'Tis good you're thinkin'这样的行。sed真的无法区分单引号和撇号。 -
甚至
'Tis good the dogs' toys were found- 完全不可能用正则表达式与'The buck stops here' is a quote之类的东西区分开来。 -
使用
’作为撇号,而不是',因为是正确的排版。