We can extend this further into XML files where we may not want to display the complete record, but just certain fields. Consider the following product catalog:
<products>
<product> <name>drill</name> <price>99</price> <stock>5</stock> </product> <product> <name>hammer</name> <price>10</price> <stock>50</stock> </product> <product> <name>screwdriver</name> <price>5</price> <stock>51</stock> </product> <product> <name>table saw</name> <price>1099.99</price> <stock>5</stock> </product>
</products>
Logically, each record is delimited as before with the empty line. Each field though is a little more detailed and we need to use the delimiter as follows: FS="[><]". We define either the opening or closing angle bracket as the field delimiter.
To help analyze this, we can print a single record as follows:
<product><name>top</name><price>9</price><stock>5</stock></product>
Each angle brace is a field separator, which means that we will have some empty fields. We could rewrite this line as a CSV file:
,product,,name,top,/name,,price,9,/price,,stock,5,/stock,,/product,
We just replace each angle bracket with a comma; in this way it is more easily read by us. We can see that the content of field 5 is the top value.
Of course, we will not edit the XML file, we will leave it in the XML format. The conversion here is just to highlight how the field separators can be read.
The control file that we use to extract data from the XML file is illustrated in the following code example:
BEGIN { FS="[><]"; RS="\n\n" ; OFS=""; }
$0 ~ search { print $4 ": " $5, $8 ": " $9, $12 ": " $13 }
Within the BEGIN code block, we set the FS and RS variables as we have discussed. We also set the Output Field Separator (OFS) or to a space. In this way, when we print the fields we separate the values with a space rather than leaving in the angle brackets. The range makes use of the same match as we used before when looking at the virtual hosts.
If we need to search for the product drill from within the catalog, we can use the command in the following example:
$ awk -f catalog.awk search=drill catalog.xml
The following screenshot shows the output in detail:

We have now been able to take a rather messy XML file and create readable reports from the catalog. The power of AWK is highlighted again and, for us, the last time within this book. By now, I hope you too can start to make use of this on a regular basis.