Kolam Ayer MakersLinux Foundations

awk

Use

awk -F: '{print $1}' /etc/passwd

What It Does

awk reads input as records, splits each record into fields, and runs a small program for each record.

By default, a record is one line and fields are separated by whitespace. -F: changes the field separator to colon.

Mental Model

awk -F: '{print $1}' /etc/passwd

Examples

Print the first colon-separated field:

awk -F: '{print $1}' /etc/passwd

Print two fields with a label:

awk -F: '{print "user=" $1 " shell=" $7}' /etc/passwd

Print line numbers using awk’s built-in NR variable:

awk '{print NR ": " $0}' ~/src/pages/index.md

Print only lines where the third field is at least 1000:

awk -F: '$3 >= 1000 {print $1}' /etc/passwd

Count matching lines:

awk '/ssh/ {count += 1} END {print count}' ~/.bash_history

Sum numbers from the first field:

printf '1\n2\n3\n' | awk '{total += $1} END {print total}'

Change output separator with OFS:

awk 'BEGIN {OFS=","} {print $1, $2}' names.txt

Use awk in a pipeline:

ps aux | awk '$3 > 10 {print $1, $2, $3, $11}'

Watch Out

Docs Pointers