【发布时间】:2016-10-30 10:37:10
【问题描述】:
有谁知道如何在 SED 命令中替换包含 \u2015 的字符串,如下例所示?
sed -ie "s/some text \u2015 some more text/new text/" inputFileName
【问题讨论】:
-
取决于
\u2015代表什么。
有谁知道如何在 SED 命令中替换包含 \u2015 的字符串,如下例所示?
sed -ie "s/some text \u2015 some more text/new text/" inputFileName
【问题讨论】:
\u2015 代表什么。
不确定这是否正是您所需要的,但也许您应该看看 native2ascii 工具来转换此类 unicode 转义。
通常它会将所有无法在 ISO-8859-1 中显示的字符替换为其 unicode(用 \u 转义),但它也支持反向转换。假设你有一些 UTF-8 文件,名为“input”,包含\u00abSome \u2015 string\u00bb,然后执行
native2ascii -encoding UTF-8 -reverse input output
将产生带有«Some ― string» 的“输出”文件。
【讨论】:
您只需要避开存在的斜线。下面的示例在GNU sed version 4.2.1 中运行良好
$ echo "some text \u2015 some more text" | sed -e "s/some text \\\u2015 some more text/abc/"
$ abc
您也不必使用-i 标志,根据man 页面仅用于就地编辑files。
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied). The default operation mode is to break symbolic and hard links. This can be changed with --follow-symlinks and
--copy.
【讨论】: