X
    Categories: Linux

Six awk command examples

awk command examples :

Awk is an text processing language which will typically use for extracting the required data from the flat file. You can define your requirements by using the conditions in the awk programming.

This is widely used product in most of the UNIX like operating systems.

To explain this I have given list of awk command examples.

Create the following file – students- using your favourite editor:

Manmohan 286 555-5574 manmohan 8
 Atul 280 555-4221 atul 10
 Anshul 279 555-1112 anshul 2
 Romel 284 555-2121 shrikant 12

Now let us take awk command examples one by one.

1. awk command examples for printing only names of all the students

[root@kalwa1 tmp]# gawk '{print $1}' students
Manmohan
Atul
Anshul
Romel
[root@kalwa1 tmp]#

2.  awk command examples for condition matching.

a. awk '$1 "~"/Anshul/ {print $0}' students

Explanation:

—> $1 : Use first column for basis of further action
—> ~ (tilde sign) : Relationship Operator which will Match string i.e. /Anshul/
—> {print $0} : print all possible fields i.e. entire rec
So the output of the above awk command example it to print all the records with all the fields having first column as Anshul.

[root@kalwa1 tmp]# awk '$1 ~ /Anshul/ {print $0}' students
Anshul 279 555-1112 anshul 2
[root@kalwa1 tmp]#

b. How many students had a value in the 5th field >= 10

Example:

[root@kalwa1 tmp]# awk '$5 >= 10 {print $0}' students
Atul 280 555-4221 atul 10
Romel 284 555-2121 shrikant 12
[root@kalwa1 tmp]#

 

c. How many students had a value in the 5th field < 10 and also had second field greater than 280.

[root@kalwa1 tmp]# awk '$5 < 10 && $2 > 280 {print $0}' students
Manmohan 286 555-5574 manmohan 8
[root@kalwa1 tmp]#

 

3. awk command examples for displaying all the records between Manmohan and Anshul

[root@kalwa1 tmp]# awk '$1 ~ /Manmohan/,/Anshul/{print $0}' students
Manmohan 286 555-5574 manmohan 8
Atul 280 555-4221 atul 10
Anshul 279 555-1112 anshul 2
[root@kalwa1 tmp]#

 

4. awk command examples for not printing all records but only fields 2 and then 1 field:

[root@kalwa1 tmp]# awk '$1 ~ /Manmohan/,/Anshul/{print $2" " $1}' students
286 Manmohan
280 Atul
279 Anshul
[root@kalwa1 tmp]#

5. awk command examples for printing the entire students database:

[root@kalwa1 tmp]# gawk {print} students
Manmohan 286 555-5574 manmohan 8
Atul 280 555-4221 atul 10
Anshul 279 555-1112 anshul 2
Romel 284 555-2121 shrikant 12
[root@kalwa1 tmp]#

6. Programming example
a. Create gawk files having commands in it.

[root@kalwa1 tmp]# cat gawk.1
{print}
[root@kalwa1 tmp]#

Now from command prompt give following command:

gawk -f gawk.1 students

Example:

[root@kalwa1 tmp]# gawk -f gawk.1 students
Manmohan 286 555-5574 manmohan 8
Atul 280 555-4221 atul 10
Anshul 279 555-1112 anshul 2
Romel 284 555-2121 shrikant 12
[root@kalwa1 tmp]#

b. Now create the following second program named it as “gawk.2”

[root@kalwa1 tmp]# cat gawk.2
BEGIN {
 print "Students for UX Techno";
 print;
 }
 {print}
[root@kalwa1 tmp]#

Now from command prompt give following command:

gawk -f gawk.2 students

Example:

[root@kalwa1 tmp]# gawk -f gawk.2 students
Students for UX Techno
Manmohan 286 555-5574 manmohan 8
Atul 280 555-4221 atul 10
Anshul 279 555-1112 anshul 2
Romel 284 555-2121 shrikant 12
[root@kalwa1 tmp]#
Related Post