小编San*_*eep的帖子

sed:替换包含反斜杠的文本模式

我有一个问题sed,我可以使用以下简单示例重新创建。

考虑以下输入文件 ( input.txt ):

C:\A\quick\brown\fox\ jumps over the lazy dog
C:\A\quick\brown\fox\ ran with the hounds
Run Code Online (Sandbox Code Playgroud)

我想生成以下输出

C:\Animal\ jumps over the lazy dog
C:\Animal\ ran with the hounds
Run Code Online (Sandbox Code Playgroud)

我尝试使用 sed 创建一个简单的 shell 脚本,但它没有执行所需的替换。这是我的脚本:

FROM_PATTERN="C:\A\quick\brown\fox\"
TO_PATTERN="C:\Animal\"

#FROM_PATTERN="C:\\A\\quick\\brown\\fox\\"       # Escaping backslash does not help either
#TO_PATTERN="C:\\Animal\\"       # Escaping backslash does not help either


sed 's/$FROM_PATTERN/$TO_PATTERN/g' input.txt
#sed 's/"$FROM_PATTERN"/"$TO_PATTERN"/g' input.txt       # Quoting the pattern does not help either
Run Code Online (Sandbox Code Playgroud)

我正在运行bash版本GNU bash, version 4.4.12(3)-release-(x86_64-unknown-cygwin)

bash sed shell-script

2
推荐指数
1
解决办法
284
查看次数

标签 统计

bash ×1

sed ×1

shell-script ×1