AWK is an interpreted programming language. It’s designed for text processing. Input is read line by line and awk script/command is executed.
Basic examples
Input processing
awk '{ }' input.txt
cat input.txt | awk '{}'
Read input.txt and print the 3rd word from each line: awk '{ print $3 }' input.txt
Read input line by line, and print each line: cat input.txt | awk '{print $0}'
Check for pattern, if found print first word: awk ' /pattern/ {print $1}' input.txt
If str matches with 2nd word, print 3rd word: awk '$2=="str" {print $3}' input.txt
awk -F'&' '$2=="str" {print $3}' input.txt
If 1st word partly matches str, then print 3rd word: awk '$1 ~ "str" {print $3}' input.txt
Ignorecase string comparision, if 2nd element is “find/Find/fInd/fiND”, then print third element :
awk '{ if (tolower($2) == "find") print $3}' input.txt
BEGIN, END and NR are some of in-built variables in AWK
awk -v name=Jerry 'BEGIN{printf "Name = %s\n", name}'
Add two numbers:
awk ' BEGIN { a = 50; b = 20; print "(a + b) = ", (a + b) }'
awk 'END { print NR }' inputfile
awk 'BEGIN{print "Begin";print "multiple"} {print "Line " NR ":" $0 }END{print "END"} inputfile
References: Tutorials Point: http://www.tutorialspoint.com/awk/