At first, AWK resembles the first three letter of ‘awkward’. Nope, it’s a scripting language developed by Aho, Weinberger, and Kernighan. Their creation gives us the ability to perform data transformation on the fly. It’s one of the must-have CLI and BASH tools to manipulate text files and databases. Here’s how it works:

a. Scans a file as array of lines
b. Compares each line, which is called ‘field’ in this language, to a given pattern
c. Performs programmatic transformation on the matched fragments of each line
d. General format: awk OPTIONS 'MATCH' {action }' INPUT or "$string" | awk OPTIONS 'MATCH' {action }' 

Here are some examples:

# Print everything in a text file
wiwi@testbox:~$ awk '{ print $0 }' /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin

# Print match indexes before the match criteria of ':'
wiwi@testbox:~$ awk -F':' '{username=$1;uid=$3;gid=$4; print username,uid,gid }' /etc/passwd
root 0 0
daemon 1 1
bin 2 2
sys 3 3
sync 4 65534
games 5 60

# search for specific users
wiwi@testbox:~$ awk '/bruce|mike|sensei/' /etc/passwd
bruce:x:2000:1020:bruce:/home/bruce:/bin/bash
mike:x:2001:1020:mike:/home/mike:/bin/bash
sensei:x:2002:1020:sensei:/home/sensei:/bin/bash

list=$(awk '{ print $0 }' /etc/passwd)
"$list"|awk -F':' '{username=$1;uid=$3;gid=$4; print username,uid,gid }'

# Print word #3 and #6 from string
echo "One Two Three Four Five Six Seven Eitght Nine Ten" | awk {'print "third word: "$3" sixth word: "$6'}

# Output
third word: Three sixth word: Six

# AWK in combination with Regex - return the line matching certain patterns
regex='/^bruce/'
echo -e "$list"|awk $regex

# Working with space or comma delimited files
# Printing first and second fields
awk -F "," '{print $1 " is at table " $2}' test.csv

kim@kimlinux:~/Downloads$ cat test.csv
john doe, 2
marie jane, 5
bruce lee, 7
tom cruise, 1

kim@kimlinux:~/Downloads$ awk -F "," '{print $1 " is at table " $2}' test.csv
john doe is at table  2
marie jane is at table  5
bruce lee is at table  7
tom cruise is at table  1