redtape2015
redtape2015

Reputation: 85

Linux search for a string in a line but replace another string in that line

If test.txt file is

GREEN Volks RUNNING
GREEN Audi STOPPED
GREEN Merc RUNNING

I can grep STOPPED and replace it with sed as following

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:

GREEN Volks RUNNING
RED Audi STOPPED
GREEN Merc RUNNING

Thanks in advance.

Upvotes: 0

Views: 54

Answers (1)

William Pursell
William Pursell

Reputation: 212158

Looks like you want:

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

Upvotes: 1

Related Questions