Thursday 17 November 2016

AWK cheat sheet


Similar to the sed cheat sheet I shared in the previous article here, this article will be an awk cheat sheet. All the examples illustrated here may not be entirely original as this is something I've compiled over the years while using awk. Without any further ado, here it goes:



AWK ExpressionDescription
awk '/l.c/{print}' /etc/hosts [do a regex match with string containing 1 cahrector b/w l & c]
awk '/l*c/{print}' /etc/hosts [regex match everything b/w charectors l & c]
awk '/[al1]/{print}' /etc/hosts [a regex match with strings containing charectors a,l or l in a line]
awk '/[0-9]/{print}' /etc/hosts [prints all lines with numbers in them]
awk '/^10./ {print}' /etc/hosts [print all lines beginning with number 10]
awk '/rs$/{print}' /etc/hosts [print all lines ending with rs]
awk '/\$25.00/{print}' somedta.txt [escaping the $ character]
awk '//{print $1, $2, $3; }' somedta.txt [print columns 1,2 & 3 with fields separated by a space]
awk '//{printf "%-10s %s\n",$2, $3 }' my_shopping.txt [imporves spacial formatting between fields]
awk '/ *\$[2-9]\.[0-9][0-9] */ { print $1, $2, $3, $4, "*" ; } / *\$[0-1]\.[0-9][0-9] */ { print ; }' food_prices.list[multiple pattern matches & awk commands separateed by ;]
"awk '/ *\$[2-9]\.[0-9][0-9] */ { printf ""%-10s %-10s %-10s %-10s\n"", $1, $2, $3, $4 ""*"" ; } / *\$[0-1]\.[0-9][0-9] */ { printf ""%-10s %-10s %-10s %-10s\n"", $1, $2, $3, $4; }' somedta.txt
"[use printf for improved formatting]
awk '/ *\$[2-9]\.[0-9][0-9] */ { print $0 "*" ; } / *\$[0-1]\.[0-9][0-9] */ { print ; }' somedta.txt [value $0 denotes entire line with awk]
awk '$3 <= 30 { printf "%s\t%s\n", $0,"**" ; } $3 > 30 { print $0 ;}' somedta.txt[match value of 3rd column & print result accordingly]
awk '($3 ~ /^\$[2-9][0-9]*\.[0-9][0-9]$/) && ($4=="Tech") { printf "%s\t%s\n",$0,"*"; } ' somedta.txt[example of using multiple conditions in single awk command]
awk '$4 <= 20 { printf "%s\t%s\n", $0,"*" ; next; } {print $0 ;}' somedta.txt[add a * at the end of the line if value in 4th column is less than or euqal to 20]
ls -l | awk '$3 != "sahil" {print}' [print files not owned by user sahil]
uname -a | awk 'hostname = $2 {print hostname}' [using variables. assigned value of 2nd field to a variable named hostname]
awk '/^example.com/ { counter=counter+1 ; printf "%s\n", counter ; }' somedta.txt [use numeric variable in a for loop to count occurrances of lines beginning with techmint.com]
awk '/^example.com/ { counter=counter+1 ;} END {printf "%s\n", counter ; }'somedta.txt [print only total number of times example.com occurs in file]
awk 'BEGIN {count=0} /^example.com/ {count+=1} END {printf "%s\n", count ;}'somedta.txt [result is same as above example but here we've used begin & end both]
ls -l | grep ^- | awk 'BEGIN {total=0} {total+=$5} END {print total/1024/1024}' [print the total size of files in current directory in MB]
awk 'BEGIN {print "this is a begin Test"} /^example.com/ { counter=counter+1 ;} END { printf "%s\n", counter ; }'somedta.txt [using begin & end. begin is executed before input lines are read. END is executed after all input lines are read]
awk '{print FILENAME}'somedta.txt [FILENAME is a built in which stores the file name. This awk command will print the file name as many times as the number of lines in the file]
awk '{print NR, "has", NF, "fields" $1}'somedta.txt [NR is number of records/rows. NF is the number of fields/columns]
awk ' END { print "Number of records in file is: ", NR } 'somedta.txt [print total number of rows]
awk -F':' '{ print $1, $4 ;}' /etc/passwd [change input filed separater]
awk -F':' '$1 == "sahil" {print}' /etc/passwd [match user sahil in passwd file & print the matching line]
awk -F':' '/sahil/ {print "user", $1,"has shell", $7}' /etc/passwd [search for user sahil in passwd file & print user name & shell]
awk -F':' '{if($1 == "sahil") print ;}' passwd [does the same as above example but using if condition]
awk -F';' '{if ($1 == "12345") {$6 = 5000;} {OFS = ";"} {print $0;}}' file.txt[if 1st column has value 12345 then change value of 6th column to 5000]
awk ' BEGIN { FS=":" ; } { print $1, $4 ; } ' /etc/passwd [change input field separater 2nd method]
awk -F':' ' BEGIN { OFS="==>" ;} { print $1, $4 ;}' /etc/passwd [change input & output field separater]
user=root ; awk "/$user/ {print}" /etc/passwd [use shell variable in an awk statement]
awk 'BEGIN{ for(count=0;count<=5;count++){ print "sometext"} }' [for loop in awk. This prints the string sometext 5 times to stdout]
awk 'IGNORECASE = 1; /SaHil/ {print ;}' somedata.txt [do a case insensitive search with AWK]
echo "sahil" | awk '{print substr ($1,1,2)}' [use substring function in awk to chop off part of a string]
awk 'sub ("example.com", "test.com",$1)'somedta.txt [replace all occurances of example.com in 1st column with sahil.com]
"awk 'BEGIN {count=0}
{ if($1 == ""example.com"")
{count++}
if(count == 3)
{ sub(""example.com"",""UNIX"",$1)}}
{ print $0}' somedta.txt
"[replace 3rd occurance of example.com with UNIX]
awk '{if ($NR%2 ==0) {print $0, "\n TESTLINE"} else {print $0}}' somedta.txt [insert the word TESTLINE in a newline after every line]
awk 'NR >3 && NR < 6 {print}' somedta.txt[print line number 4 & 5 from the file]
awk '!/^$/' somedta.txt [remove blank lines from a file]
awk 'NR%2{printf "%s ",$0;next;}1' somedta.txt[join 2 line. replace newline with a space]
awk '{printf $0;printf " "}NR % 2 ==0 {print " "}' somedta.txt[join 2 line. replace newline with a space]
awk '{printf $0;printf " "}NR % 3 ==0 {print " "}' somedta.txt[join 3 line. replace newline with a space]
awk '{ print $NF }' somedta.txt [print last column in a file]
df -hTP | awk '{gsub(/%/,"")}1 {print $1,$6}' [Replace the % character with a space from df -h output]
df -hTP | awk '{gsub(/%/,"",$6)}1 {print $1,$6}' [Replace the % character with a space from df -h output limited to 6th column]

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...