Sed:搜索字符串以查找特定行,然后替换该行中的不同文本

时间:2022-03-23 16:50:13

I have a file with many different lines in it and I need a way to find specific lines:

我有一个包含许多不同行的文件,我需要一种方法来查找特定的行:

user=omega group=plasma account=test jobname=STDIN queue=omega_node

I want to use sed to search for "account=test" then replace "group=plasma" with "group=solid". I know how to do a "sed -i" and do the replacement but I can't figure out a way to do a search then pass replacement parameters.

我想用sed搜索“account = test”然后用“group = solid”替换“group = plasma”。我知道如何做一个“sed -i”并做替换,但我无法找到一种方法来进行搜索然后传递替换参数。

1 个解决方案

#1


2  

You can make a conditional substitution using this syntax:

您可以使用以下语法进行条件替换:

sed '/account=test/s/group=plasma/group=solid/' file

This only performs the substitution on lines which match the pattern account=test.

这仅在匹配模式account = test的行上执行替换。

#1


2  

You can make a conditional substitution using this syntax:

您可以使用以下语法进行条件替换:

sed '/account=test/s/group=plasma/group=solid/' file

This only performs the substitution on lines which match the pattern account=test.

这仅在匹配模式account = test的行上执行替换。