如果匹配模式,我需要用sed替换整行.
例如,如果该行是’一二二四四’并且如果’六’那么,那么整行应该用’fault’替换.
例如,如果该行是’一二二四四’并且如果’六’那么,那么整行应该用’fault’替换.
您可以使用以下任一方法执行此操作:
原文链接:/bash/387213.htmlsed '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 solution至Replace 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