Sunday, December 13, 2015

AWK Commands for Linux

1: Field/text processor
2: Default Field-delimiter is white-space.
3: Stores fields (columns) into tokens, which then become accessible during processing
4: Will accept input from: file or STDIN or pipe


Commands:

1: To print each line in its entirety.
# awk '{ print $0 }' test.txt
2: To print column #1 from each line.
# awk '{print $1}' test.txt
3: To print column #2 from each line.
# awk '{print $2}' test.txt
4: To print column #1 then #2.
# awk'{print $2,$1}' test.txt
5: To print all columns where line includes 'Red'.
# awk'/Red/ {print $0}' test.txt
6: To print column #1,#2,#3 where line include 'Red'.
# awk '/Red/ {print $1, "-",$2,"-",$3}' test.txt
7: To print all columns of records containing '2012' in the second column.
# awk '{if ($2 ~ /2012/) print $0}' test.txt
8: To print lines ending in : 2012.
# awk '/2012$/ {print $0}' test.txt
9: Accept a pipe using above point 8 commands.
# grep 2012 /var/log/messages | awk '/2012/ {print $0}'
10: To print columns $3-$6 where column 2 = '9'.
# awk '{if ($2 ~ /9/) print $3,$4,$5,$6}' /var/log/message

No comments:

Post a Comment