Re: grep sed replace

"Davide Brini [email protected] [sed-users]" <[email protected]> Thu, 10 Nov 2016 15:15:32 +0100
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On Thu, 10 Nov 2016 14:03:10 +0000, "MOKRANI Rachid [email protected]
[sed-users]" <[email protected]> wrote:

> Hi, (It's been a long time)
> 
> I have some files.
> 
> cat A.txt
> Paul Brown
> monday
> 0.8
> Mike Blue
> monday
> 0.8
> Tom Red
> friday
> 0.8
> Virginie Rose
> saturday
> 0.8
> David Brown
> saturday
> 0.7
> Brian Yellow
> monday
> 0.55
> Tom Black
> thuesday
> 0.4
> 
> 
> cat B.txt
> Bill White
> thusday
> 0.8
> Manu Purple
> monday
> 0.8
> William Grey
> thuesday
> 0.7
> Nathalie Golden
> monday
> 0.8
> Marie Pink
> sunday
> 0.6
> Susan Navy
> sunday
> 0.6
> 
> 
> I want to search in this files the string "Tom Red" and change ONLY the 3
> third lines after the result. In this example the string is in the file
> "A.txt" and need to change "0.8" by "MY_NEW_STRING"
> 
> grep -A 3 "Tom Red" *.txt (it's OK)
> 
> But How to replace ONLY the third string after ""Tom Red"  ?

You can do

sed '/Tom Red/{ n; n; s/.*/MY_NEW_STRING/; }'

-- 
D.