shell – 当找到与sed匹配时替换整行

如果匹配模式,我需要用sed替换整行.
例如,如果该行是’一二二四四’并且如果’六’那么,那么整行应该用’fault’替换.
您可以使用以下任一方法执行此操作:
sed 's/.*six.*/fault/' file     # check all lines
sed '/six/s/.*/fault/' file     # matched lines -> then remove

它获得包含六个的完整行并用故障替换它.

例:

$cat file
six
asdf
one two six
one isix
boo
$sed 's/.*six.*/fault/'  file
fault
asdf
fault
fault
boo

它基于this solutionReplace whole line containing a string using Sed

更一般地,您可以使用表达式sed’/match/s/.*/replacement/’文件.这将在包含匹配的行中执行sed的/ match / replacement /’表达式.在你的情况下,这将是:

sed '/six/s/.*/fault/' file

What if we have ‘one two six eight eleven three four’ and we want to
include ‘eight’ and ‘eleven’ as our “bad” words?

在这种情况下,我们可以将-e用于多个条件:

sed -e 's/.*six.*/fault/' -e 's/.*eight.*/fault/' file

等等.

或者:

sed '/eight/s/.*/XXXXX/; /eleven/s/.*/XXXX/' file

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...