Cyberithub

60 Popular Examples of AWK Command in Linux Part - 1

Advertisements

In this article, I will take you through 60 Popular Examples of AWK Command in Linux Part - 1. AWK is a very powerful tool for handling large amount of data(especially when it comes to parsing data files). AWK along with Sed Command found many of its uses in Bash Scripting. To Know more about Sed Command, you can check 60 Popular Examples of Sed Command in Linux Part - 1.

AWK was created at Bell Labs in the 1970s, and its name is derived from the family names of its authors - Alfred Aho, Peter Weinberger and Brian Kernighan.

Why should we use AWK 

It can perform below functions:-
a)It can handle a text file as made up of records and fields in a textual database.
b)It can Perform arithmetic and string operations
c)It can use programming constructs such as loops and conditionals.
d)It can also produce formatted reports.

60 Popular Examples of AWK Command in Linux Part - 1 1

Important AWK Builtin Variables

NF: The number of fields in the current input record.

NR: The total number of input records seen so far.

OFMT: The output format for numbers, "%.6g", by default.

OFS: The output field separator, a space by default.

ORS: The output record separator, by default a newline.

RS: The input record separator, by default a newline.

RT: The record terminator.

AWK Command Examples

Also Read: How to Print Array in Bash Script

1. Check AWK Command Version

To check the awk command version, you need to run awk -V command as shown below. As you can see from below output, current installed awk version is 4.0.2.

[root@localhost ~]# awk -V
GNU Awk 4.0.2
Copyright (C) 1989, 1991-2012 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

2. Check AWK Command Copyright Details

If you want to know more about copyright details, you can always run awk -C command and check that.

[root@localhost ~]# awk -C
Copyright (C) 1989, 1991-2012 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

3. Print a Sorted List of Global Variables

If you want to print out a sorted list of all the global variables, then you need to use -d option with awk command as shown below.

[root@localhost ~]# awk -d ' '

-d [file]: Print a sorted list of global variables, their types and final values to file. If no file is provided, gawk uses a file named awkvars.out in the current directory.

By default, all the variables will be dumped in awkvars.out file if you don't specify the file name.

[root@localhost ~]# ls awkvars.out
awkvars.out
[root@localhost ~]# cat awkvars.out
ARGC: 1
ARGIND: 0
ARGV: array, 1 elements
BINMODE: 0
CONVFMT: "%.6g"
ERRNO: ""
FIELDWIDTHS: ""
FILENAME: ""
FNR: 0
FPAT: "[^[:space:]]+"
FS: " "
IGNORECASE: 0

4. Print Contents of /etc/passwd file based on Colon Separation(:)

If you simply want to view contents of /etc/passwd through awk command, you can do that by running below awk command.

[root@localhost ~]# awk -F ":" '{ print }' /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

-F: Use fs for the input field separator (the value of the FS predefined variable).

5. Print 1st Column Separated by Colon(:)

If you want to print only the 1st column from /etc/passwd file based on colon(:) separation, then you need to use  -F option and print out the $1 contents which necessarily means 1st column contents separated by colon(:).

[root@localhost ~]# awk -F ":" '{ print $1 }' /etc/passwd
root
bin
daemon
adm
lp
sync

$1: First Column

$2: Second Column

$3: Third Column

$4: Fourth Column

$5: Fifth Column

and so on.

6. Print 3rd Column Separated by Colon(:)

Similarly, if you want to print out the 3rd column data, then you need to use print $3 in parentheses which means 3rd column based on colon(:) separation from /etc/passwd file.

[root@localhost ~]# awk -F ":" '{ print $3 }' /etc/passwd
0
1
2
3
4
5

7. Print 1st and 3rd Column Separated by Colon(:)

If you want to print out only 1st and 3rd Column contents, then you can query both $1 and $3 simultaneously as shown below.

[root@localhost ~]# awk -F ":" '{ print $1,$3 }' /etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5

8. Print 1st and 3rd Column Using OFS Comma(,)  

If you want to print 1st and 3rd column contents using Output field separator(OFS) , you can do that by using below command. You can notice here that output contents are separated by comma(,).

[root@localhost ~]# awk -F ":" '{ OFS="," } { print $1,$3 }' /etc/passwd
root,0
bin,1
daemon,2
adm,3
lp,4
sync,5

9. Print last Column using NF Variable

If you want to print last column using number of field(NF) variable, you can do that by simply using $ with NF variable as shown below under print statement.

[root@localhost ~]# awk -F ":" '{ print $(NF) }' /etc/passwd
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync

10. Print Second Last Column Using NF Variable

If you want to print second last column using NF variable, then you can do that by using below command. As you know /etc/passwd file contains 7 columns, so basically NF - 1 means 7 - 1 = 6 which tells awk to print out all the contents from 6th column.

[root@localhost ~]# awk -F ":" '{ print $(NF-1) }' /etc/passwd
/root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin

11. Print First Column of /etc/passwd Using NF Variable

If you want to print 1st column using NF variable, you can do that by using below command. As you know $NF always contains the last column number and we know there are 7 columns in /etc/passwd, so by this information NF - 6 =7 - 6 = 1. Hence it is basically telling to print all the contents from 1st Column of /etc/passwd file.

[root@localhost ~]# awk -F ":" '{ print $(NF-6) }' /etc/passwd
root
bin
daemon
adm
lp
sync

12. Print 6th Column along with line number Using NR Variable

If you want to print all the data of 6th Column with Line number using Number of Record(NR) variable, you can do that by simply using below command.

[root@localhost ~]# awk -F ":" '{ print NR,$6 }' /etc/passwd
1 /root
2 /bin
3 /sbin
4 /var/adm
5 /var/spool/lpd
6 /sbin

13. Match and Print 'bash' keyword from /etc/passwd

If you want to match bash keyword from file /etc/passwd and print all the lines containing this keyword, you can do that by using /bash/ matching pattern as shown below.

[root@localhost ~]# awk '/bash/ {print} ' /etc/passwd
root:x:0:0:root:/root:/bin/bash
zookeeper:x:1000:1000::/home/zookeeper:/bin/bash

14. Match and Print 'bash' or 'root' keyword from /etc/passwd

If you want to print all the lines which has either root or bash keyword in /etc/passwd file, then you need to use /root|bash/ matching pattern as shown below.

[root@localhost ~]# awk '/root|bash/ {print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dockerroot:x:997:994:Docker User:/var/lib/docker:/sbin/nologin
zookeeper:x:1000:1000::/home/zookeeper:/bin/bash

15. Print 7th Column of Every Line having keyword 'root' 

If you want to print 7th column of every line which has the keyword root using if statement, then you need to use comparison operator(==) as shown below. Here you can see from the output, only one line contains keyword root in /etc/passwd file.

[root@localhost ~]# awk -F ":" '{if($1 == "root") print $7}' /etc/passwd
/bin/bash

16. Print 7th Column of every Line which does not contains keyword 'root' 

If you want to print 7th Column of every line which does not have root keyword, then you need to use below command. Notice that here we have used not equal to operator(!=) in the parentheses to not match the specific keyword.

[root@localhost ~]# awk -F ":" '{if($1 != "root") print $7}' /etc/passwd
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin

17. Count the number of Lines in /etc/passwd file

You can use NR variable to count the number of Lines as shown below. You can just do print NR in the END statement and it will show you the number of total lines.

[root@localhost ~]# awk 'END { print NR }' /etc/passwd
24

18. Run System Command through AWK

If you want to run any of linux command using awk, then you need to pass that command as an argument to system function as shown below. You can find more info AWK Man Page.

[root@localhost ~]# awk 'BEGIN { system("pwd") }'
/root

19. Print 7th Column Every Line which starts with keyword 'ntp'

If you want to print 7th column of every line which contains keyword ntp at the start of the line, then you need to use ^ntp as shown below.

[root@localhost ~]# awk -F ":" '/^ntp/{print $7}' /etc/passwd
/sbin/nologin

20. Print 1st Column of Every Line which ends with Keyword '/sbin/nologin'

If you want to print 1st Column of every line which has /sbin/nologin at the end of the line, then you need to use below command.

[root@localhost ~]# awk -F ":" '/\/sbin\/nologin$/{print $1}' /etc/passwd
bin
daemon
adm
lp
mail
operator
games
ftp
nobody

 

 

Also Read: 5 Tools to create bootable USB from CLI

Leave a Comment