AWK Commands

AWK and CUT commands and their usage

AWK commands

Before going to look at the commands and their usage lets understand why we need AWK, what was the requirement which led us to use AWK commands. So if we need to print some particular column from a file then we use AWK. Here are few examples:

$> awk ‘{print $6}’ abc.tcl ##To print the 6th word from abc.tcl.

$> awk ‘{print $4,$6}’ abc.tcl ##To print the 4th and the 6th word from abc.tcl.

$> awk ‘{print $4 “\t” $6}’ abc.tcl ##This Perform same as above but with a tab gap in between.

$> awk ‘/create_power/ {print}’ design.upf ##To print only the lines having the word create_power.

Looking at above command we can have below commands which has same meaning.

$> awk ‘/create_power/ {print $0}’ design.upf
$> awk ‘/create_power/ {print}’ design.upf
$> awk ‘/create_power/’ design.upf
 
$> awk ‘/create_power/{++cnt} END {print cnt}’ design.upf # Display the number of occurrences of the word create_power.

If one wants to print all the lines greater than a particular number, then use below command.

awk ‘length > <Number>’ <Filename>

For eg. The below two commands print the lines whose length is greater than 15 from all the lines present in file.

$> awk ‘length > 15’ design.upf
$> awk ‘length($0) > 15’ design.upf

If one wants to print the count of the lines whose length is greater than 15 from all the lines present in the file.

$> awk ‘length > 15 {++cnt} END {print cnt}’ 2

If you want to kill a job and you have all the job id’s in column then we can use below command and then source it, which will kill all the jobs where job id was given.

$> awk ‘{print “bkill” $1}’ $1=job ID

If you want to cut the line using awk command, then use the below. The pattern presents in between double quote will split the lines and cut it from there. Let’s see the command below.

$> awk -F “:” ‘{print $1}’

CUT Command

CUT command will be used same as above command we have used with the help of AWK command.

$> cut -d “:” -f1

Gaurav Sharma
Gaurav Sharma

Leave a Reply