Generating reports with awk

Back in Chapter 6, we showed you how to edit delimited files with awk, which is cool because it lets you extract specific pieces of information, such as names and phone numbers, from delimited files. As shown in Code Listing 16.2, you can also use awk to generate reports. We start with the information from an ls -la command, then use awk to generate a report about who owns what.

To generate reports with awk:

  • ls -la | awk '{print $9 " owned by " $3 } END { print NR " Total Files" }'

    Whew! In general, pipe ls -la to the long-winded awk command. (Yes, this is the origin of awkward.) awk then prints the ninth field ($9), the words "owned by," then the third field ($3), and at the end of the output, the total number of records processed (print NR " Total Files"). Code Listing 16.2 shows the printed report.

Tip

Remember that you could embed awk scripts in a shell script, as with the previous sed example, if it's something you'll use frequently.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.7.197