I have following text file.
banana apple juice mango something
I am searching for pattern
juice, and I want to find the 2nd line from that matching pattern in reverse order (i.e 2 lines above the matching pattern) and replace it with
coconut apple juice mango something
I tried with following, but it just deletes the above two line and not the exact one I'm looking for.
tac foo.txt |sed '/juice/I,+2 d' |tac mango something
I think tweaking above script would do the job, but I am not sure.
Note: There will not be any re-occurrence of the match, and it needs not to be an exact match (meaning, the match can be found in a long line as well). The match should be case-sensitive.