Linux - 替换字符串

标签 linux shell sed grep

我需要为该字符串/行的每个实例替换一个字符串/行。 在以下行中:

set output "/DS/tmp/2.gnuplot.ps" 

我需要它是:

set output "./gnuplot.ps" 

我想使用:

grep -rl 'stringONE' ./ | xargs sed -i 's/stringONE/stringTWO/g'

但是当我尝试使用它时,它与字符串中的/发生冲突...

grep -rl '/DS/tmp/2.gnuplot.ps' ./ | xargs sed -i 's//DS/tmp/2.gnuplot.ps/./gnuplot.ps/g'

任何帮助将不胜感激!!!

最佳答案

如果要替换的字符串包含斜杠 (/),那么您可以用其他内容分隔 sed 命令的参数,在本例中我使用逗号:

grep -rl '/DS/tmp/2.gnuplot.ps' ./|xargs sed -i 's,/DS/tmp/2.gnuplot.ps,./gnuplot.ps,g'

关于Linux - 替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922166/

相关文章:

linux - 有没有办法从 linux 的系统日志中检索消息的严重级别?

bash - 我们可以通过 AWS SSM 将命令作为后台进程运行吗?

unix - 使用 sed 捕获第一个大写字母的单词

bash - 在数组中找到最小的缺失整数

c++ - Linux 上的共享库版本和可执行文件

linux - 仅在尚未发送时通知发送

shell - 在 jenkins 管道上提取字符串的一部分

c - 脚本 : insert an additional #include after the last #include in many, 许多文件

c - Gtk-ERROR ** : GTK+ 2. 检测到 x 符号

bash - `test -n $a` 和 `test -n "$a"` 之间的区别