Awk command with simple examples

Now, we are going to learn very interesting and Most important  in Linux
Why late lets know about Awk command,one of the mostly used command in scripting. awk command used to manipulate data | using this AWK command we can generate specified reports as per our requirement.





Syntax :

awk ‘BEGIN {start_action} {action} END {stop_action}’ filename

let me explain about awk command with simple examples

Example 1:
How to print entire  data of the file

cat filename | awk '{print}'
cat filename | awk '{print$0}'

both "awk '{print}' and awk '{print$0}' " used to print entire date

Example 2:
How to print  very first column

cat filename|  awk '{print$1}'

awk '{print$1}' used to print very first column  
awk '{print$1}' used to print second column  

Example 3:
How to print pattern Matching Technique in Awk.

free -m | awk '{/Mem/print}'

here, we are matching Mem using awk '{/Mem/print}'

Example 4:

How to Print multiple column at the time.

free -h | awk '/Mem/{print$1,$2,$4}'

here, we are print 1st,2nd,3rd columns at the time.

Example 5:

How to Print line number using variable NR

free -h | awk '{print NR,$0}'

Example 6:
How to print specific lines

free -h | awk 'NR == 2,NR == 9{print NR,$0}'

Here,we are print lines between 2 & 9.

Thanks you.  

Real time
ll -lrth | awk '{print$9}'

Filter data manipulation
cat listofusers.txt | awk '$4~/M/'


cat listofusers.txt | awk '{printNF}'
cat listofusers.txt | awk '{print (NF-1)}' //second last coloum


Comments

  1. I need small help i have received one task on from my team can you help one this

    1) • The script should take the server list from a file as input. Basically, server list should contain the OA(Onboard Administrator) IP address. Username/Password to access the OA is assumed to be
    2)• The script should take each OA IP from the server list file and login to the OA, and identify the failed hosts. The following command can be used for that.

    ReplyDelete

Post a Comment

Popular posts from this blog

Learn Linux in Telugu | Linux complete Free Course in Telugu by 7Hills

rsync Command Examples | rsync Command In Telugu