Linux sed,替换第一行的第一个匹配项
我的文本如下所示Linux sed,替换第一行的第一个匹配项,linux,bash,awk,sed,Linux,Bash,Awk,Sed,我的文本如下所示 this This that it It Its my My Mine this This that it It Its my My Mine 我想替换第一个匹配项的第一行。匹配包含my的行,然后替换该行。是的 cat txt|sed "0,/my/c\my changed line" txt 打印输出如下所示,前两行被修剪 my changed line this This that it It Its my My Mine 如果我运行此cat txt | sed“s/
this This that
it It Its
my My Mine
this This that
it It Its
my My Mine
我想替换第一个匹配项的第一行。匹配包含my
的行,然后替换该行。是的
cat txt|sed "0,/my/c\my changed line" txt
打印输出如下所示,前两行被修剪
my changed line
this This that
it It Its
my My Mine
如果我运行此cat txt | sed“s/my/changeline/”txt
输出如下
this This that
it It Its
changeline My Mine
this This that
it It Its
changeline My Mine
我怎样才能得到下面这样的结果
this This that
it It Its
changeline My Mine
this This that
it It Its
my My Mine
使用
sed
:
sed '0,/.*my.*/s//my changed line/' file
这是做什么的,,
在0的范围内,/.*my.*/
它将用“我的更改行”替换匹配的*my.*
相同内容的一个相对容易理解的版本:
sed '0,/my/{/.*my.*/s//my changed line/}' file
使用awk
逻辑更容易理解:
awk '!/my/ || seen { print } /my/ && !seen { print "my changed line"; seen = 1 }' file
使用
sed
:
sed '0,/.*my.*/s//my changed line/' file
这是做什么的,,
在0的范围内,/.*my.*/
它将用“我的更改行”替换匹配的*my.*
相同内容的一个相对容易理解的版本:
sed '0,/my/{/.*my.*/s//my changed line/}' file
使用awk
逻辑更容易理解:
awk '!/my/ || seen { print } /my/ && !seen { print "my changed line"; seen = 1 }' file