Skip to content

awk

awk 'script' filename | where 'script' is a set of commands that are understood by awk

Bash
awk <'script'> filename

Basic

Bash
awk '//{print}' /etc/hosts
  • filter | pattern
Bash
awk '/localhost/{print}' /etc/hosts
  • filter c*m | example all words with com cam cem etc
Bash
awk '/c.m/{print}' /etc/hosts
  • use *
Bash
awk '/l*c/{print}' /etc/hosts
  • Take for example the set [al1], here awk will match all strings containing character a or l or 1 in a line in the file /etc/hosts.
Bash
awk '/[al1]/{print}' /etc/hosts
  • matches strings starting with either K or k followed by T
Bash
awk '/[Kk]T/{print}' /etc/hosts 
  • It matches all the lines that start with the pattern
Bash
awk '/^ff/{print}' /etc/hosts
  • It matches all the lines that end with the pattern
Bash
awk '/ost$/{print}' /etc/hosts

Standard use cases

  • filter out row | print second row (NAME) | docker service
Bash
docker service ls | awk '{print $2}'