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 ...

Get Unix: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.