Issue
From the many hits about searching a pattern and printing the lines until the end of the pattern, I've encountered an issue. The solution works if there is no other entry that is similar or has the common numbers in the pattern. Let me explain.
startpat=30.202.10.0
endpat=31.202.10.0
inputfile is as follows:
30.202.10.0
abcd
efgh
31.202.10.0
ijkl
mnop
130.202.10.0
qrst
uvwx
awk -v startpat="${startpat}" -v endpat="${endpat}" '$0 ~ startpat{ x = 1} $0 ~ endpat{ x = 0}x' inputfile
I get the expected output. The problem arises when the data contains as an example 130.202.10.0. The result contains lines
abcd
efgh
qrst
uvwx
How do I get as follows
abcd
efgh
Solution
Current code:
$ awk -v startpat="${startpat}" -v endpat="${endpat}" '$0 ~ startpat{ x = 1} $0 ~ endpat{ x = 0}x' inputfile
30.202.10.0
abcd
efgh
130.202.10.0
qrst
uvwx
Assuming the patterns (startpat
and endpat
) are not to be printed, and blank lines are not to be printed, one awk
idea:
$ awk -v startpat="${startpat}" -v endpat="${endpat}" '$1==startpat {x=1;next} $1==endpat {x=0;next} x && NF>0' inputfile
abcd
efgh
Answered By - markp-fuso Answer Checked By - Pedro (WPSolving Volunteer)