简体   繁体   English

Linux在一行中搜索一个字符串,但替换该行中的另一个字符串

[英]Linux search for a string in a line but replace another string in that line

If test.txt file is 如果test.txt文件是

GREEN Volks RUNNING
GREEN Audi STOPPED
GREEN Merc RUNNING

I can grep STOPPED and replace it with sed as following 我可以grep停止并用sed替换它,如下所示

grep "STOPPED" test.txt |sed 's/^GREEN/RED/g'

Which returns following output 返回以下输出

RED Audi STOPPED

But instead I would like try something like 但是我想尝试类似

sed -i 's/^GREEN/RED/g'| grep "STOPPED" test.txt
to see the output as following: 看到如下输出:

\nGREEN Volks RUNNING 绿色Volks跑步\nRED Audi STOPPED 红色奥迪停止\nGREEN Merc RUNNING 绿色商品跑步\n

Thanks in advance. 提前致谢。

看起来像你想要的:

  sed '/STOPPED/s/^GREEN/RED/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM