awk works on programs that contain rules comprised of patterns and actions. The action is executed on the text that matches the pattern. Patterns are enclosed in curly braces ({}). Together, a pattern and an action form a rule. The entire awk program is enclosed in single quotes (').

Here is the output of -h

Perhaps i don’t need all the output I want only mounted file systems to print and it size let say.

df -h |grep -v Filesystem |awk ‘{ print $2 ” ” $NF }’

In the above we are ignoring first description line using grep –v. Then printing required values .Infact we can print as many values as we want . NF holds last values .

$0 represents entire line 
$1 represents the first field 
$2 represent second field
$7 represent 7th field
$NF represents the last record.

In the previous command if we don’t specify delimiter then space is the default .  so df –h delimited with space then we are printing the values.Also we used “ “ space when printing .If we use , then space used while printing.

df -h |grep -v Filesystem |awk ‘{ print $2 , $NF }’

Let’s see different delimiter “:” with simple echo passing to awk

echo “ABC:DEF:XYZ” |awk –F”:” ‘{ print $1,$2,$NF }’

In this both NF and $3 are having same value .

Another example just want to print the date in Mon DD YYYY format

date | awk ‘{print $2,$3,$6}’

OFS –> Output field Separator

date | awk 'OFS="-" {print$2,$3,$6}'

The BEGIN and END Rules

A BEGIN rule is executed once before any text processing starts. In fact, it’s executed before awk even reads any text. An END rule is executed after all processing has completed. You can have multiple BEGIN and END rules, and they’ll execute in order.

awk 'BEGIN {print "File Systems"} {print $NF}' /tmp/dfout.txt

Adding pattern’s or Conditions

We can also add patterns before printing with AWK . In the below example we will check if the 3rd value greater or equal 1000 the print those rows or fields on those rows .

Combining both BEGIN and patterns on awk

In case if we want to develop our own logic using awk follow below

The first line of the script tells the shell which executable to use

#!/usr/bin/awk -f

  # set the input and output field separators
  # zero the accounts counter
  # set field 2 to nothing
  # print the entire line
  print $0
  # count another account
  # print the results
  print accounts " accounts.\n"

Hope this article help to use awk for data processing with advance utility …

Related Posts

Leave a Reply

Your email address will not be published.