5:40 PM AWK command for scanning and analysing a large file in Linux |
awk - A command for pattern scanning and processing of a large file
Field Annotations
1. To print the entire file. awk '{print;}' employee.txt Pompt:>awk '{print;}' employee.txt Name EmpId City sex age Shankar 624532 Hyderabad Male 24 Priyanka 624534 Bangalore Female 24 Anirudh 62543 Chennai Male 24 Sagar 363533 Pune Male 23 Bhavani 624531 Lahore Female 22 2. To print the line containing 23 or print employees of age 23. Pompt:>awk '/23/' employee.txt Sagar 363533 Pune Male 23 3.To print the line containing 23 or 22 ; or print all employees of age 23 or 22 Note:- All the search patterns are given in new line starting with '>' Pompt:>awk '/23/ > /22/' employee.txt Sagar 363533 Pune Male 23 Bhavani 624531 Lahore Female 22 4. Print only 2nd column of the file or print only the employee Ids Pompt:>awk '{print $2}' employee.txt EmpId 624532 624534 62543 363533 624531 5. Print only 2nd and 4th column of the file or print only the employee Ids and Sex Pompt:>awk '{print $2, $4}' employee.txt EmpId sex 624532 Male 624534 Female 62543 Male 363533 Male 624531 Female 6. Print all the male employees. ie. 4th column is matching with 'Male' Pompt:>awk '$4~/Male/{print;}' employee.txt Shankar 624532 Hyderabad Male 24 Anirudh 62543 Chennai Male 24 Sagar 363533 Pune Male 23 7. Print all the employee whose age is above or equal to 23. Pompt:>awk '$5>=23{print;}' employee.txt Name EmpId City sex age Shankar 624532 Hyderabad Male 24 Priyanka 624534 Bangalore Female 24 Anirudh 62543 Chennai Male 24 Sagar 363533 Pune Male 23 8. Print the no. of employees who are in Hyderabad. Pompt:>awk 'BEGIN {count=0;} > $3~/Hyderabad/ {count++;} > END {print "No of employees in Hyderabad=", count;}' employee.txt No of employees in Hyderabad= 2 9. Remove duplicate lines from a file
The above command will use awk tool to search for a duplicate line, if it found again, it will not be printed.
10. Print all lines from /etc/passwd that has the same uid and gid
Here in, we have checked if the 3rd columns matches the 4th columns, If yes, then they are printed. ":" is used as the seperator delimeter. 11. Read AWK instructions from a program-file and perform on target file (-f option). -f program-file
Here the awkfile is containing:
So the above awk will search the targetfile and if second column matches shankar, it will store 3rd column $3 in array arr. Later for each data in the array, a script somescript is executed with array value as argument. |
|
Related blogs
You may also like to see:
[2015-06-03] | [Open System-Linux] |
STAT command : check file or filesystem statistics |
[2014-03-11] | [Open System-Linux] |
STRINGS : a command in linux to read the non-text files |
[2014-09-24] | [Open System-Linux] |
Set, unset, display shell behaviour options : SHOPT command in Linux |
[2017-08-13] | [Open System-Linux] |
Perl trick to remove Ctrl - M charater from a file |
[2014-03-12] | [Open System-Linux] |
uptime command in Linux |
Total comments: 2 | |
| |