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:
[2016-05-11] | [Open System-Linux] |
An example to understand bash exit code $? in linux. |
[2014-09-21] | [Open System-Linux] |
How to define command prompt strings in Linux : what are $PS1, $PS2, $PS3 and $PS4? |
[2014-03-25] | [Open System-Linux] |
Create a new user in Linux system: useradd |
[2015-06-08] | [Open System-Linux] |
CHAGE command in LINUX |
[2014-03-03] | [Open System-Linux] |
Working with VI Editor: Basic and Advanced |
Total comments: 2 | |
| |