【问题标题】:Replace a full line, if that line has matched string (Solaris #!/usr/xpg4/bin/sh)替换整行,如果该行匹配字符串 (Solaris #!/usr/xpg4/bin/sh)
【发布时间】:2016-03-15 15:44:14
【问题描述】:
我有一个文本文件,其中包含类似的特定行
sometext sometext sometext SEARCHED sometext sometext sometext
我需要用
替换上面的整行
modified due to security issue
如果SEARCHED 在上面的行中找到
我需要为此编写一个 shell 脚本。如何使用 sed for solaris #!/usr/xpg4/bin/sh shell 来实现这一点?
【问题讨论】:
标签:
shell
sed
sh
solaris
solaris-10
【解决方案1】:
这是一个 awk 版本:
nawk '/SEARCHED/ {
print "modified due to security issue";next}
1' file > file.patched
【解决方案2】:
当你想编辑原始文件时,使用ed:
# I do not know if your shell supports <<<
ed -s textfile <<< $',s/.*SEARCHED.*/modified due to security issue/g\nw'
当您不能使用上述解决方案时,请使用几行:
ed -s textfile << EOF
,s/SEARCHED/modified due to security issue/g
w
q
EOF