Topic 10

Date: 3/22/2017
Linux shell scripting tutorial
Linux System Administration

Introduction to AWK

The basic function of awk is to search files for lines or other text units containing one or more patterns. When a line matches one of the patterns, special actions are performed on that line.

Display user names from /etc/passwd (field 1):
awk -F: '{ print $1 }' /etc/passwd
Where F is the field separator; in passwd file, fields are separated by ':' Default field separator is a blank space. Awk scans the input file and splits each input line into fields. Similarly:
cat /etc/passwd | awk -F: '{ print $1 }'

Display user names home directories and login shell (fields 1 and 7): and store them in a separate file, users.txt
awk -F: '{ print $1, $6, $7 }' /etc/passwd > users.txt
cat /etc/passwd | awk -F: '{ print $1, $6, $7 }' > users.txt

Default field separator is empty space. To print users (field 1) from just created file users.txt:
awk '{ print $1 }' users.txt

Recommended tutorial: The GNU awk programming language

Take me to the Course Website