Back to course

59. Introduction to `awk` (Field Processing)

Linux Basics: From Zero to CLI Hero

Advanced Data Processing

awk (named after its developers Aho, Weinberger, and Kernighan) is a programming language designed for pattern scanning and processing, particularly excellent for handling structured data (like tables or CSV files).

How awk Works

It processes a file line by line, and for each line, it treats fields (columns) as variables, usually delimited by whitespace.

  • $0: Represents the entire line.
  • $1: Represents the first field.
  • $2: Represents the second field, and so on.

Basic Field Extraction

Print the first and last name from a list, assuming they are the first and second fields:

bash $ echo -e "John Doe\nJane Smith" | awk '{print $1, $2}' John Doe Jane Smith

Specifying a Field Separator (-F)

Just like cut, awk can use a different field separator (delimiter).

Example: Extract the username (Field 1) and UID (Field 3) from /etc/passwd (delimiter is :):

bash $ awk -F ':' '{print "User: " $1, " UID: " $3}' /etc/passwd User: root UID: 0 User: daemon UID: 1 ...