Skip to content

awk command

Process columns of text: print fields, filter rows and do sums.

On this page

Syntax

awk [options] 'pattern { action }' [file…]

Provided by the gawk / mawk package. Run man awk for the full manual.

Common options

OptionWhat it does
-F SEPField separator, e.g. -F: or -F,
-v VAR=VALPass a variable into the program
$1, $2 …Fields; $0 is the whole line
$NFThe last field
NRCurrent line number
NFNumber of fields on the line

Examples

  1. Print the first column

    awk '{print $1}' access.log
  2. Use a different separator

    awk -F: '{print $1}' /etc/passwd
  3. Print the last column

    awk '{print $NF}' file.txt
  4. Rows where column 3 is above 100

    awk '$3 > 100' data.txt
  5. Sum a column

    awk '{sum += $5} END {print sum}' sizes.txt
  6. Count requests per IP

    awk '{print $1}' access.log | sort | uniq -c | sort -rn | head
  7. Print lines 5 to 10

    awk 'NR>=5 && NR<=10' file.txt
  8. Print fields with a custom format

    awk -F, '{printf "%-20s %s\n", $1, $2}' users.csv

awk guides

← All Linux commands · Suggest a fix

Back to top