AWK — The Unix/Linux Powerful tool.

What is AWK?

What can we do with AWK?

awk [options] 'selection _criteria ' <<file1>>Pattern-action statement:
awk 'pattern {action}' file_name
E.g ; Print first column of non-empty row. OR Delete empty fields.
NF : Number of fields
$awk 'NF>1 { print $1 } test.txt

Some of the built-in variable commonly use in AWK:-

# Print 3 column of test.data file.
$ awk '{ print $3 }' test.data
# Print columns 4 and 1.
$ awk '{ print $4, $1 }' test.data
# Number each line
$ awk '{ print FNR $0}'
# Number of line with tab
$ awk '{ print FNR "\t" $0}' test.data // $0 denotes all fields
# Count lines ; ( same a wc -l )
$ awk 'END {print NR}' test.data //NR: Line number
# Print last field of each line.
$ awk '{ print $NF}' test.data
# Print last field of last line.
$ awk 'END { print $NF }' test.data
# Print every line, where the value of 4th filed is more than 10.
$ awk '$4>10{ print }' test.data
# Print the lines starting from 10.
$ awk 'NR >9 { print } test.data
# Seperate with field Seperator.
$ awk -F "--" { print $2 } test.data
# Print alternate lines.
$ awk 'NR%2' test1.data
# Omit the third line.
$ awk 'NR%3' test.data
# Substitute ABC by XYZ.
$awk '{sub(/ABC/,"ARG")}; print' test.data

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store