Is a /start/,/end/ range expression ever useful in awk?

Interesting. I also often start with a range expression and then later on switch to using a variable..

I think a situation where this could be useful, aside from the pure range-only situations is if you want to print a match, but only if it lies in a certain range. Also because it is immediately obvious what it does. For example:

awk '/start/,/end/{if(/ppp/)print}' file

with this input:

start
dfgd gd
ppp 1
gfdg
fd gfd
end
ppp 2 
ppp 3
start
ppp 4
ppp 5
end
ppp 6
ppp 7
gfdgdgd

will produce:

ppp 1
ppp 4
ppp 5


One could of course also use:

awk '/start/{f=1} /ppp/ && f; /end/{f=0}' file

But it is longer and somewhat less readable..

Leave a Comment