I need to replace the whole line with sed
if it matches a pattern. For example if the line is 'one two six three four' and if 'six' is there, then the whole line should be replaced with 'fault'.
如果匹配模式,我需要用sed替换整行。例如,如果该行是'一二二四四'并且如果'六'那么,那么整行应该用'fault'替换。
3 个解决方案
#1
47
You can do it with either of these:
您可以使用以下任一方法执行此操作:
sed 's/.*six.*/fault/' file # check all lines
sed '/six/s/.*/fault/' file # matched lines -> then remove
It gets the full line containing six
and replaces it with fault
.
它获得包含六个的完整行并用故障替换它。
Example:
例:
$ cat file
six
asdf
one two six
one isix
boo
$ sed 's/.*six.*/fault/' file
fault
asdf
fault
fault
boo
It is based on this solution to Replace whole line containing a string using Sed
它基于此解决方案使用Sed替换包含字符串的整行
More generally, you can use an expression sed '/match/s/.*/replacement/' file
. This will perform the sed 's/match/replacement/'
expression in those lines containing match
. In your case this would be:
更一般地,您可以使用表达式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?
如果我们有'一二*十一三四'并且我们想把'八'和'十一'作为我们的“坏”字怎么办?
In this case we can use the -e
for multiple conditions:
在这种情况下,我们可以将-e用于多个条件:
sed -e 's/.*six.*/fault/' -e 's/.*eight.*/fault/' file
and so on.
等等。
Or also:
或者:
sed '/eight/s/.*/XXXXX/; /eleven/s/.*/XXXX/' file
#2
3
This might work for you (GNU sed):
这可能适合你(GNU sed):
sed -e '/six/{c\fault' -e ';d}' file
or:
要么:
sed '/six/{c\fault'$'\n'';d}' file
#3
3
Above answers worked fine for me, just mentioning an alternate way
上面的答案对我来说很好,只是提到另一种方式
Match single pattern and replace with a new one:
匹配单个模式并替换为新模式:
sed -i '/six/c fault' file
Match multiple pattern and replace with a new one(concatenating commands):
匹配多个模式并替换为新模式(连接命令):
sed -i -e '/one/c fault' -e '/six/c fault' file
#1
47
You can do it with either of these:
您可以使用以下任一方法执行此操作:
sed 's/.*six.*/fault/' file # check all lines
sed '/six/s/.*/fault/' file # matched lines -> then remove
It gets the full line containing six
and replaces it with fault
.
它获得包含六个的完整行并用故障替换它。
Example:
例:
$ cat file
six
asdf
one two six
one isix
boo
$ sed 's/.*six.*/fault/' file
fault
asdf
fault
fault
boo
It is based on this solution to Replace whole line containing a string using Sed
它基于此解决方案使用Sed替换包含字符串的整行
More generally, you can use an expression sed '/match/s/.*/replacement/' file
. This will perform the sed 's/match/replacement/'
expression in those lines containing match
. In your case this would be:
更一般地,您可以使用表达式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?
如果我们有'一二*十一三四'并且我们想把'八'和'十一'作为我们的“坏”字怎么办?
In this case we can use the -e
for multiple conditions:
在这种情况下,我们可以将-e用于多个条件:
sed -e 's/.*six.*/fault/' -e 's/.*eight.*/fault/' file
and so on.
等等。
Or also:
或者:
sed '/eight/s/.*/XXXXX/; /eleven/s/.*/XXXX/' file
#2
3
This might work for you (GNU sed):
这可能适合你(GNU sed):
sed -e '/six/{c\fault' -e ';d}' file
or:
要么:
sed '/six/{c\fault'$'\n'';d}' file
#3
3
Above answers worked fine for me, just mentioning an alternate way
上面的答案对我来说很好,只是提到另一种方式
Match single pattern and replace with a new one:
匹配单个模式并替换为新模式:
sed -i '/six/c fault' file
Match multiple pattern and replace with a new one(concatenating commands):
匹配多个模式并替换为新模式(连接命令):
sed -i -e '/one/c fault' -e '/six/c fault' file