Main menu

Basics of AWK with examples

AWK (Aho, Weinberger, and Kernighan) stand for it’s creators name . AWK is a text processing and reporting tool used for printing, finding, replacing the strings. Let’s see some AWK with examples
In UNIX or LINUX everything is a file, and better you can process a file better you can administrate. Like getting reports from logs, filtering the data etc.

AWK is a default problem on many Linux distributions created by AT&T. GAWK or Gnu AWK is an GNU version of AWK program having some advantages of AWS. AT&T also created a new AWS program called NAWK which is default on many UNIX distributions like solaris and BSD.

 

AWK with examples “Print statement”

AWK has a print command for printing any output on terminal. AWK process each lines one by one and print the line or columns which match the expression. If no argument is passed with AWK it will print entire line.

awk '{ print }' /etc/passwd

$0 variable hold the entire line so when you call AWK to print $0 it will print entire line one by one in PASSWD file. It’s same as if no argument is specified with print statement.

awk '{ print $0}' /etc/passwd

Let’s print the first column of PASSWD file. We have used -F for defining the filed separator as by default AWK use spaces and tabs as filed separator. Then we printed the $1 which hold the first column of input.

awk -F":" '{ print $1}' /etc/passwd

-F : Field separator

Same if we want to print multiple columns we can simply put another column number is print command

awk -F":" '{ print $1 $7}' /etc/passwd

Input user data in output : We have inserted data before each files.

awk -F":" '{print "user :" $1 "shell :"$7}' /etc/passwd

Using Escape char in AWK : Here we use /t which will generate a tab between both fields.

awk -F":" '{print "user :" $1 "/t" shell :"$7}' /etc/passwd

Other Escape char

  • \b Backspace
  • \f Form feed
  • \n Newline (line feed)
  • \r Carriage return
  • \t Tab
  • \<special char> like \\ or \*

AWK with examples “Search”

AWK has a search pattern

awk '/pattern1/ {Actions} /pattern2/ {Actions}' file

We can search multiple string at the same time and define multiple actions for the search result on AWK.

Let’s search for string call bash on every line of PASSWD File and only print column 1 and column 7 or those lines.

awk -F":" '/bash/ {print $1 $7}' /etc/passwd

AWK with examples “The BEGIN and END blocks”

AWK has two major sections or blocks BEGIN and END. We start with BEGIN and specify an action that will execute before processing any lines then we process the lines. After AWK finish it’s job we call the END block.

BEGIN { Action before processing anything}
 {Process the input}
 END { Action after the processing everything }

Let’s print some string in BEGIN and END section.

awk -F":" 'BEGIN {print "UserName\tDescription\tLoginShell";}
 {print $1,"\t",$5,"\t",$7}
 END{print "--------Report Ends\n--------------";}' /etc/passwd

Working with operators

  • < Less than
  • <= Less than or equal to
  • == Equal to
  • != Not equal to
  • >= Greater than or equal to
  • > Greater than
  • ~ Matched by (used when comparing strings)
  • !~ Not matched by (used when comparing strings)

Users who have id greater then 500. Third column in passwd file holds the user id of usrs. if it’s greater then 500 then prints the lines.

awk -F":" 'BEGIN {print "\nUserName\tDescription\tLoginShell";}
 $3>500 {print $1,"\t",$5,"\t",$7}
 END{print "--------Report Ends--------------\n";}' /etc/passwd

Print if 7th column having the word nologin. ~ operator compare with regular expressions.

awk -F":" 'BEGIN {count=0; print "\nUserName\tDescription\tLoginShell";}
 $7 ~ /nologin/ {print $1,"\t",$5,"\t",$7}
 END{print "--------Report Ends--------------\n";}' /etc/passwd

Counting the matched pattern: – We start with count veriable and define it to zeor. On every match AWK will increase the count variable by one. At the end of all processing we will print the final count variable in END section.

awk -F":" 'BEGIN {count=0;}
 $7 ~ /nologin/ {count++;}
 END{print "Total nologin users: -", count;}' /etc/passwd

Continue..

FacebookTwitterGoogle+RSS