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

前端之家收集整理的这篇文章主要介绍了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

猜你在找的Bash相关文章