AWK - saviovettoor/DevOps-wiki GitHub Wiki

Extract fields 2, 4, and 5 from file.txt:

awk '{print $2,$4,$5}' input.txt

Print each line where the 5th field is equal to ‘a5’:

awk '$5 == "a5"' file.txt

Print each line whose 5th field start with any a/b/c:

awk '$5  ~ /^[a-c]/' file.txt

Remove duplicates while preserving order

awk '!rdupe[$0]++' file.txt

Sum a column:

awk '{ sum+=$1} END {print sum}' file.txt

print the last field of each line

awk '{ print $NF }' file.txt

print the last field of the last line

awk '{ field = $NF }; END{ print field }' file.txt

Count lines (emulates "wc -l")

awk 'END{print NR}' file.txt

precede each line by its line number FOR ALL FILES TOGETHER, with tab.

awk '{print NR "\t" $0}' files*

Merge two files and throwh out the result

awk '{print $0}' file1.txt file2.txt