How do I print the contents of an XML element - from the starting tag to the closing tag - using AWK?
For example, consider the following XML:
$ awk -v tag='city' '$0~"^<"tag"\\>"{inTag=1} inTag; $0~"^"tag">"{inTag=0}' file
Athens
GA
Home of the University of Georgia
100,000
Located about 60 miles Northeast of Atlanta
33 57' 39" N
83 22' 42" W
Using GNU awk above for \>
word boundary functionality. With other awks use [^[:alnum:]_]
or similar.
To only print the first occurrence:
$ awk -v tag='city' '$0~"^<"tag"\\>"{inTag=1} inTag{print; if ($0~"^"tag">") exit}' file
Athens
GA
Home of the University of Georgia
100,000
Located about 60 miles Northeast of Atlanta
33 57' 39" N
83 22' 42" W