Sometimes it is hard to locate the exact line you want to modify if there are so many lines having the exact same codes/text.

example:
#this is x
a=0
#this is y
#this is yy
a=0
this is z
a=0

My question is, is it possible to find something unique, such as "#this is y" from above, and then modify the "a=0" 2 lines down?

this has been puzzling me..

thanks

Recommended Answers

All 2 Replies

k2k,

This sounds similar to what you're asking in your other thread. So you want to match the line TWO down from the first match instead of the one directly below?

Will there ever be an 'a=0' between them? like this:

#this is y
a=0
a=0

where you would want to replace only the second a=0? Or will it always be something like:

#this is y
something else here
a=0

If there will always be something else there, the sed statement in your other thread should work just fine. It's not only matching two *lines*, it's matching two *expressions*, so the work will be done on every line between those two expressions.

For example, using your sample text:

-> sed '/#this is y$/,/a=/ {s/a=0/a=1/}' tmp.txt 
#this is x
a=0
#this is y
#this is yy
a=1
this is z
a=0

this is another idea I have but not sure if it is doable...

what I am trying to ask is if there is anyway to modify x line number above or below a matched line.

example:
I know I am able to find a unique line "#this is a comment x"
I want to modify 3 lines down to make it "#this is a newly replaced line" no matter what this line is.

#this is a comment x   --> go down 3 line and make it "#this is a newly replaced ln"
line 1
line 2
line 3    --> i am hoping if there is a way that I don't have to care what this line is, as long as it is 3 line down from the matched line, then it will be replaced.

I couldn't google anything close... thanks, maybe I should focus on the other thread on how to match patten of 2 lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.