I have a file that looks like the following:
Pat1
Id1.1 IP1.1 Desc1.1
Id1.2 IP1.2 Desc1.2
Id1.3 IP1.3 Desc1.3
Id1.4 IP1.4 Desc1.4
Id1.5 IP1.5 Desc1.5
Pat2
Id2 IP2 Description2
Pat3
Id3 IP3 Description3
Pat4
Id4 IP4 Description4
Pat5
Id5 IP5 Description5
Pat6
Id6 IP6 Description6
and the file goes on. What I am interested in doing is extracting the IPs between Pat1 and Pat2, Pat3 and Pat4, and Pat5 and Pat6. So, the output should look like after some reformatting:
Output:
range1, IP1.1
range1, IP1.2
range1, IP1.3
range1, IP1.4
range1, IP1.5
range2, IP3
range3, IP5
I have written the following code, but I would like to shrink it by using less code to get the same output. Please take a look at the code below:
awk 'flag{ if (/Pat2/){printf "%s", buf; flag=0; buf=""} else buf = buf $0 ORS}; /Pat1/{flag=1}' ripe.txt > IPs.txt
awk '!/^$/' IPs.txt > IPs.csv
awk '!/[a-z]/' IPs.csv > tmp && mv tmp IPs.csv
awk '{print "range1,"$2}' IPs.csv > done.csv
awk 'flag{ if (/Pat4/){printf "%s", buf; flag=0; buf=""} else buf = buf $0 ORS}; /Pat3/{flag=1}' ips.txt > A1.txt
awk '!/^$/' IPs.txt > IPs.csv
awk '!/[a-z]/' IPs.csv > tmp && mv tmp IPs.csv
awk '{print "range2,"$2}' IPs.csv >> done.csv
awk 'flag{ if (/Pat6/){printf "%s", buf; flag=0; buf=""} else buf = buf $0 ORS}; /Pat5/{flag=1}' ips.txt > A1.txt
awk '!/^$/' IPs.txt > IPs.csv
awk '!/[a-z]/' IPs.csv > tmp && mv tmp IPs.csv
awk '{print "range3,"$2}' IPs.csv >> done.csv
As you see, there is a repetitive chunk of code that I would like to reduce by iterating through the patterns pairs with the flexibility of adding range1, range2, and range3 as column1 before each IP range. Thanks in advance.