Thursday 21 April 2016

How to use unix tool AWK

This artical will help you understand how to work with the AWK utility in UNIX. It also gives
the meaning of some of the AWK Built-in Variables


These few AWK one liners give very basic and random examples which will help to understand basic about this UNIX tool.

Meaning of some of the Awk Built-in Variables used below:

NF               : Number of fields in current line/record

NR               : Ordial number of current line/record
FS                : Field Separator (Also -F can be used)
OFS             : Output Field Separator (default=blank)

FILENAME : Name of current input file

All of following Awk one liner examples is based on the input file 'test1.txt’

$ cat test1.txt                                                               
Continent:Val
AS:12000
AF:9800                                                               
AS:12300
NA:3400
OC:12000
AF:500
AS:1000

Know more bout UNIX FILE PERMISSIONS   Click here


Scenario
Print 'line number' NR and 'Number of fields' NF for each line
Command
awk -F ":" '{print NR,NF}' test1.txt
Output
1 2

2 2

3 2

4 2

5 2

6 2

7 2

8 2


Scenario
Print first field, colon delimited
Command
awk -F ":" '{print $1}' test1.txt

Continent

AS

AF

AS

NA

OC

AF

AS


Scenario
Print first field, colon delimited, but excluding the 'first line' (NR!=1)
Command
awk -F ":" 'NR!=1 {print $1}' test1.txt
Output
AS

AF

AS









NA

OC

AF

AS


Scenario
Print first field, colon delimited, but only for line number 1 (NR==1)
Command
awk -F ":" 'NR==1 {print $1}' test1.txt
Output
Continent


Scenario
Print first and second field, colon delimited, but excluding the 'first line' (NR!=1)
Command
awk -F ":" 'NR!=1 {print $1,$2}' test1.txt
Output
AS 12000

AF 9800

AS 12300

NA 3400



OC 12000

AF 500

AS 1000


Scenario
Setting output field separator as pipe
Command
awk -F ":" 'BEGIN{OFS="|"} NR!=1 {print $1,$2}' test1.txt
Output
AS|12000

AF|9800

AS|12300

NA|3400

OC|12000

AF|500

AS|1000


Scenario
Anything on BEGIN executes first
Command
awk 'BEGIN{FS=":"; OFS="|"; print "Con|SomeVal"} NR!=1 {print $1,$2}' test1.txt
Output
Con|SomeVal

AS|12000

AF|9800

AS|12300

NA|3400

OC|12000

AF|500

AS|1000


Scenario
Printing FILENAME, will be printed for all the lines
Command
awk -F ":" '{print FILENAME}' test1.txt
Output
test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt

test1.txt



Scenario
Printing FILENAME, but printing only last instance using END clause
Command
awk -F ":" ' END {print FILENAME}' test1.txt
Output
test1.txt


Scenario
Printing the last field of the file, same as printing $2 as there are only 2 fields
Command
awk -F ":" '{print $NF}' test1.txt
Output
Val

12000

9800

12300

3400

12000

500

1000


Scenario
Matching, printing lines begin with "AS"
Command
awk -F ":" '/^AS/' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
Matching, printing lines not begining with "AS"
Command
awk -F ":" '!/^AS/' test1.txt
Output
Continent:Val

AF:9800

NA:3400

OC:12000

AF:500


Scenario
Direct matching, first field as "AS"
Command
awk -F ":" '$1=="AS"' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
Direct matching, first field as "AS", Print 2nd Column
Command
awk -F ":" '$1=="AS" {print $2}' test1.txt
Output
12000

12300

1000


Scenario
$0 prints the full line, same as {print}
Command 1
awk -F ":" '$1=="AS" {print $0}' test1.txt
Output
AS:12000

AS:12300

AS:1000





Command 2
awk -F ":" '$1=="AS" {print}' test1.txt
Output
AS:12000

AS:12300

AS:1000


Scenario
'Or' and 'AND' together
Command
awk -F ":" '($1=="AS" || $1=="OC") && $NF > 11000 {print}' test1.txt
Output
AS:12000

AS:12300

OC:12000


Scenario
Partial Matching
Command
awk -F ":" '$1 ~ /A/ {print}' test1.txt
Output
AS:12000

AF:9800

AS:12300

NA:3400

AF:500

AS:1000


Scenario
Reading from STDOUT
Command
cat test1.txt | awk -F ":" '!/Continent/ {print $1}' | sort | uniq
Output
AF

AS

NA

OC


Scenario
Add value 1000 to the 2nd field, where first field is "AF" and then print the output file
Command
awk -F ":" '$1=="AF" {$2+=1000} {print}' test1.txt
Output
Continent:Val

AS:12000

AF 10800

AS:12300

NA:3400

OC:12000

AF 1500

AS:1000


Scenario
Sum of 2nd fields, exclude first line
Command
awk -F ":" 'NR!=1 {sum+=$NF} END {print sum}' test1.txt
Output
51000


Scenario
Set 2nd value as 0 where first field is "AS"
Command
awk -F ":" 'BEGIN {OFS=":"} $1=="AS" {$2=0} {print}' test1.txt
Output
Continent:Val

AS:0

AF:9800

AS:0

NA:3400




1 comment:

  1. replica rolex watches, combining elegant style and cutting-edge technology, a variety of styles of https://my.desktopnexus.com/sarahhearduk/journal/replica-rolex-watches-28934, the pointer walks between your exclusive taste style.

    ReplyDelete