O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

One-liners for system administrators

In this section, we look at the workings of various one-liners used for the automation of system administration tasks in shell scripts and day-to-day admin tasks.

  • Print and sort the login name of all users on Linux OS:
$ awk -F ":" '{ print $1 | "sort" }' /etc/passwd
  • List all filenames whose size is greater than zero:
$ ls -al | awk '$5 > 0{ print $9} '
  • Print all lines of a file prefixed with a line number:
$ awk '{print NR, $0}' <FILENAME>
  • Calculate and print the total size of a directory in Mb:
$ ls -al | awk '{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}'
  • Calculate and print the total size of a directory, including sub directories, in Mb:
$ ls -lR |awk '{total +=$5};END {print ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required