Pages

Friday, January 1, 2016

awk command one liners (basic)

Home

Adding “Double space” in output for the provided input file

# head -n 5 /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
#

# head -n 5 /etc/passwd |awk -F ':' '1;{print ""}'
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

#

# head -n 5 /etc/passwd |awk -F ':' 'BEGIN{ORS="\n\n"};1'
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

#

Double space a file which already has blank lines in it. Output file should contain no more than one blank line between lines of text.
NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are often treated as non-blank, and thus 'NF' alone will return TRUE.

# head -n 5 /etc/passwd |awk -F ':' 'NF{print $0 "\n"}'
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

#

Adding “Triple space” in output for the provided input file

 # head -n 5  /etc/passwd |awk -F ':' '1;{print "\n"}'
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


#

Precede each line by its line number FOR THAT FILE (left alignment). Using a tab (\t) instead of space will preserve margins.

# head -n 5 /etc/passwd |awk -F ':' '{print FNR "\t" $0}'
1       root:x:0:0:root:/root:/bin/bash
2       bin:x:1:1:bin:/bin:/sbin/nologin
3       daemon:x:2:2:daemon:/sbin:/sbin/nologin
4       adm:x:3:4:adm:/var/adm:/sbin/nologin
5       lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


Precede each line by its line number FOR ALL FILES TOGETHER, with tab.

# head -n 5 /etc/passwd |awk -F ':' '{print NR "\t" $0}'
1       root:x:0:0:root:/root:/bin/bash
2       bin:x:1:1:bin:/bin:/sbin/nologin
3       daemon:x:2:2:daemon:/sbin:/sbin/nologin
4       adm:x:3:4:adm:/var/adm:/sbin/nologin
5       lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

Number each line of a file (number on left, right-aligned) double the percent signs if typing from the DOS command prompt.

# cat /etc/login.defs |grep -v '#'

MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000

GID_MIN                   500
GID_MAX                 60000


CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes

ENCRYPT_METHOD SHA512

#

# cat /etc/login.defs |grep -v '#' |awk -F ':' '{printf("%5d : %s\n", NR,$0)}'
    1 :
    2 : MAIL_DIR        /var/spool/mail
    3 :
    4 : PASS_MAX_DAYS   99999
    5 : PASS_MIN_DAYS   0
    6 : PASS_MIN_LEN    5
    7 : PASS_WARN_AGE   7
    8 :
    9 : UID_MIN                   500
   10 : UID_MAX                 60000
   11 :
   12 : GID_MIN                   500
   13 : GID_MAX                 60000
   14 :
   15 :
   16 : CREATE_HOME     yes
   17 :
   18 : UMASK           077
   19 :
   20 : USERGROUPS_ENAB yes
   21 :
   22 : ENCRYPT_METHOD SHA512
   23 :


Number each line of file, but only print numbers if line is not blank Remember caveats about UNIX treatment of \r (mentioned above)

# cat /etc/login.defs |grep -v '#'

MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000

GID_MIN                   500
GID_MAX                 60000


CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes

ENCRYPT_METHOD SHA512

#

# cat /etc/login.defs |grep -v '#' |awk 'NF{$0=++a " :" $0};{print}'

1 :MAIL_DIR     /var/spool/mail

2 :PASS_MAX_DAYS        99999
3 :PASS_MIN_DAYS        0
4 :PASS_MIN_LEN 5
5 :PASS_WARN_AGE        7

6 :UID_MIN                        500
7 :UID_MAX                      60000

8 :GID_MIN                        500
9 :GID_MAX                      60000


10 :CREATE_HOME yes

11 :UMASK           077

12 :USERGROUPS_ENAB yes

13 :ENCRYPT_METHOD SHA512

#

# cat /etc/login.defs |grep -v '#' |awk '{print (NF? ++a " :" :"") $0}'

1 :MAIL_DIR     /var/spool/mail

2 :PASS_MAX_DAYS        99999
3 :PASS_MIN_DAYS        0
4 :PASS_MIN_LEN 5
5 :PASS_WARN_AGE        7

6 :UID_MIN                        500
7 :UID_MAX                      60000

8 :GID_MIN                        500
9 :GID_MAX                      60000


10 :CREATE_HOME yes

11 :UMASK           077

12 :USERGROUPS_ENAB yes

13 :ENCRYPT_METHOD SHA512

#

Count lines (simulate "wc -l")

# wc -l /etc/passwd
67 /etc/passwd

# awk 'END{print NR}' /etc/passwd
67

Print the sums of the fields of every line

# cat /etc/login.defs |grep -v '#' |head -n 5

MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0

# cat /etc/login.defs |grep -v '#' |head -n 5 | awk  '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'
0
0
0
99999
0

Add all fields in all lines and print the sum

# cat /etc/login.defs |grep -v '#' |head -n 5 | awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'
99999

Print every line after replacing each field with its absolute value

# cat /etc/login.defs |grep -v '#' |head -n 5 | awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'

MAIL_DIR 0

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0

# cat /etc/login.defs |grep -v '#' |head -n 5 | awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'

MAIL_DIR 0

PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0

Print the total number of fields ("words") in all lines

# awk '{ total = total + NF }; END {print total}' /etc/passwd
119
# wc  /etc/passwd
  67  119 3568 /etc/passwd

Print the total number of lines that contain "nologin"

# awk '/nologin/{n++}; END {print n+0}' /etc/passwd
51
# grep nologin /etc/passwd |wc -l
51

Search the largest first field and the line that contains it intended for finding the longest string in field #1

# awk '$1 > max {max=$3; maxline=$0}; END{ print max, maxline}' /etc/passwd
 spark:x:477:471:Spark:/var/lib/spark:/sbin/nologin

To print complete line from provided input

# awk -F ':'  '{print  $0 } ' /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

To print number of fields for each line

# cat /etc/login.defs |grep -v '#'|awk '{ print NF ":" $0 } '
2:MAIL_DIR      /var/spool/mail
0:
2:PASS_MAX_DAYS 99999
2:PASS_MIN_DAYS 0
2:PASS_MIN_LEN  5
2:PASS_WARN_AGE 7
0:
2:UID_MIN                         500
2:UID_MAX                       60000
0:
2:GID_MIN                         500
2:GID_MAX                       60000
0:
0:
2:CREATE_HOME   yes
0:
2:UMASK           077
0:
2:USERGROUPS_ENAB yes

To print last field and second last field of each line from input

# head -n 5 /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

# awk -F ':' '{print $NF ":" $(NF-1) }' /etc/passwd
/bin/bash:/root
/sbin/nologin:/bin

Search every line with more than 4 fields

# awk -F ':' 'NF > 4' /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

Search every line where the value of the last field is equal to ‘/bin/bash’

# awk -F ':' '$NF == "/bin/bash"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

Switch the first 2 fields of every line

# head -n 5 /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

# head -n 5 /etc/passwd |awk -F ':' '{temp = $1; $1 = $2; $2 = temp; print $0}'
x root 0 0 root /root /bin/bash
x bin 1 1 bin /bin /sbin/nologin
x daemon 2 2 daemon /sbin /sbin/nologin
x adm 3 4 adm /var/adm /sbin/nologin
x lp 4 7 lp /var/spool/lpd /sbin/nologin

Search every line, deleting the second field of that line

# head -n 5  /etc/passwd |awk -F '"' '{ $2 = ""; print $0}'
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

# head -n 5  /etc/passwd |awk -F ':' '{ $2 = ""; print $0}'
root  0 0 root /root /bin/bash
bin  1 1 bin /bin /sbin/nologin
daemon  2 2 daemon /sbin /sbin/nologin
adm  3 4 adm /var/adm /sbin/nologin
lp  4 7 lp /var/spool/lpd /sbin/nologin

Search in reverse order the fields of every line

# head -n 5 /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

# head -n 5 /etc/passwd |awk -F ':' '{for (i=NF; i>0; i--) printf("%s ",$i);printf ("\n")}'
/bin/bash /root root 0 0 x root
/sbin/nologin /bin bin 1 1 x bin
/sbin/nologin /sbin daemon 2 2 x daemon
/sbin/nologin /var/adm adm 4 3 x adm
/sbin/nologin /var/spool/lpd lp 7 4 x lp

Remove duplicate, consecutive lines (simulate "uniq")

# cat admin/test.file
test.file
123
123
234
234
test.file
345
345
123
345
234
test.file

# cat admin/test.file |awk -F ':' 'a !~ $0; {a=$0}'
test.file
123
234
test.file
345
123
345
234
test.file

Remove duplicate, nonconsecutive lines

# cat admin/test.file
test.file
123
123
234
234
test.file
345
345
123
345
234
test.file

Most concise command line shown below,

# cat admin/test.file  |awk '! a[$0]++'
test.file
123
234
345

Most efficient command line shown below,

# cat admin/test.file  |awk '!($0 in a) {a[$0];print}'
test.file
123
234
345    

Search first 10 lines of file (simulate behavior of "head")

# head /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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:

# cat /etc/passwd |awk 'NR < 11'
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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:

Search first line of file (simulate "head -1")

# awk 'NR>1{exit};1' /etc/passwd
root:x:0:0:root:/root:/bin/bash

# head -n 1 /etc/passwd
root:x:0:0:root:/root:/bin/bash

Search the last 2 lines of a file (simulate "tail -2")

# awk '{y=x "\n" $0; x=$0};END{print y}' /etc/passwd
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

# tail -n 2 /etc/passwd
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

Search the last line of a file (simulate "tail -1")

# awk 'END{print}' /etc/passwd
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

# tail -n 1  /etc/passwd
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

Search only lines which match regular expression (simulate "grep")

awk '/regex/'

Regular expression to search lines starting with letter [a,b,c]
# awk '/^[a-c]/' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin

Search only lines which do NOT match regex (simulate "grep -v")

awk '!/regex/'

Regular expression to search lines not starting with letter between [a to s]

# awk '!/^[a-s]/' /etc/passwd
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin

Search the line immediately before a regex, but not the line containing the regex

awk '/regex/{print x};{x=$0}'  file

Regular expression to search lines starting with letter between [a to c]

# awk '/^[a-c]/{print x};{x=$0}'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
distcache:x:94:94:Distcache:/:/sbin/nologin

Below grep commands gives the line satisfying the regex along with a line before it.

# cat /etc/passwd |grep '^a' -B 1
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
--
dbus:x:81:81:System message bus:/:/sbin/nologin
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
--
distcache:x:94:94:Distcache:/:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
# cat /etc/passwd |grep '^b' -B 1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
# cat /etc/passwd |grep '^c' -B 1
#

# cat /etc/login.defs |grep -v '#'| awk '/^[p-z]/{print (x=="" ? "match on line 1" : x)};{x=$0}'
match on line 1
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
match on line 1
UID_MIN                   500
match on line 1
match on line 1

Search the line immediately after a regex, but not the line containing the regex

awk '/regex/{getline;print}'

Regular expression to search lines starting with letter between [a to c]

# awk '/^[a-c]/{getline;print}'  /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin

Below grep commands gives the line satisfying the regex along with a line after it.

# cat /etc/passwd |grep '^a' -A 1
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
--
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
--
apache:x:48:48:Apache:/var/www:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
# cat /etc/passwd |grep '^b' -A 1
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
# cat /etc/passwd |grep '^c' -A 1
#

Grep for some words

# cat /etc/login.defs |grep -v '#'
MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000

GID_MIN                   500
GID_MAX                 60000


CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes

# cat /etc/login.defs |grep -v '#'|awk '/PASS/;/MAX/'
PASS_MAX_DAYS   99999
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MAX                 60000
GID_MAX                 60000

# cat /etc/login.defs |grep -v '#'|awk '/PASS|MAX/'
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MAX                 60000
GID_MAX                 60000

Grep for some words using regex

# cat /etc/login.defs |grep -v '#'
MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000

GID_MIN                   500
GID_MAX                 60000


CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes

# cat /etc/login.defs |grep -v '#'|awk '/MAX.*PASS/'
PASS_MAX_DAYS   99999
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MAX                 60000
GID_MAX                 60000

# cat /etc/login.defs |grep -v '#'|awk '/PASS|MAX/'
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MAX                 60000
GID_MAX                 60000

Search only lines of 65 characters or longer

# awk 'length > 64' /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

Search only lines of less than 31 characters

# awk 'length < 30' /etc/passwd
news:x:9:13:news:/etc/news:

Search section of file from regular expression to end of file

awk '/regex/,0'

Regular expression to search lines starting with letter [w]

# awk '/^w/,0' /etc/passwd
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin


awk '/regex/,EOF'

Regular expression to search lines starting with letter [w]

# awk '/^v/,EOF' /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
distcache:x:94:94:Distcache:/:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin

Search section of file based on line numbers (lines 8-12, inclusive)

awk 'NR==8,NR==12'

# awk 'NR==8,NR==12' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

# cat -n /etc/passwd|grep halt
     8  halt:x:7:0:halt:/sbin:/sbin/halt
# cat -n /etc/passwd|grep mail
     9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    20  mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
# cat -n /etc/passwd|grep news
    10  news:x:9:13:news:/etc/news:
# cat -n /etc/passwd|grep uucp
    11  uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
# cat -n /etc/passwd|grep operator
    12  operator:x:11:0:operator:/root:/sbin/nologin

Search line number 30

# awk 'NR==30' /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin

More efficient way to use it
# awk 'NR==30 {print;exit}' /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin

Search section of file between two regular expressions (inclusive)

# awk '/news/,/operator/' /etc/passwd
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

Delete ALL blank lines from a file (same as "grep '.' ")

# cat /etc/login.defs |grep -v '#' |awk 'NF'
MAIL_DIR        /var/spool/mail
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MIN                   500
UID_MAX                 60000
GID_MIN                   500
GID_MAX                 60000
CREATE_HOME     yes
UMASK           077
USERGROUPS_ENAB yes

# cat /etc/login.defs |grep -v '#' |awk '/./'
MAIL_DIR        /var/spool/mail
PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7
UID_MIN                   500
UID_MAX                 60000
GID_MIN                   500
GID_MAX                 60000
CREATE_HOME     yes
UMASK           077
USERGROUPS_ENAB yes

Original Input to AWK commands mentioned above,

# cat /etc/login.defs |grep -v '#'
MAIL_DIR        /var/spool/mail

PASS_MAX_DAYS   99999
PASS_MIN_DAYS   0
PASS_MIN_LEN    5
PASS_WARN_AGE   7

UID_MIN                   500
UID_MAX                 60000

GID_MIN                   500
GID_MAX                 60000


CREATE_HOME     yes

UMASK           077

USERGROUPS_ENAB yes

Search the input for lines containing a specific string:

# awk '/nologin/' /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Search only some fields of the input. This can also be done with grep or sed:

# awk -F ':' '/nologin/ { print "USERNAME: " $1 " GID: " $3 " UID: " $4}' /etc/passwd
USERNAME: bin GID: 1 UID: 1
USERNAME: daemon GID: 2 UID: 2
USERNAME: adm GID: 3 UID: 4
USERNAME: lp GID: 4 UID: 7
USERNAME: mail GID: 8 UID: 12
USERNAME: uucp GID: 10 UID: 14
USERNAME: operator GID: 11 UID: 0
USERNAME: games GID: 12 UID: 100
USERNAME: gopher GID: 13 UID: 30
USERNAME: ftp GID: 14 UID: 50
USERNAME: nobody GID: 99 UID: 99

Search the input for the lines beginning with a specific word:

# awk '/^root:/' /etc/passwd
root:x:0:0:root:/root:/bin/bash

Search the input for the lines beginning with a specific word and containing a predefined string (In example given below, its starting with r and ending with nologin):

# awk '/^r.*nologin/' /etc/passwd
rpm:x:37:37::/var/lib/rpm:/sbin/nologin
rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

Search the input for the lines by finding a string under specific conditions (In example given below, it’s searching line which has value in 3rd column less than 20):

# awk -F ':' '{if ($3 <= 20) 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
halt:x:7:0:halt:/sbin:/sbin/halt

Search the input for the lines by finding a string under specific conditions (In example given below, its searching line which has value in 3rd column less than 20, and Searching only Username, GID, UID):

# awk -F ':' '{if ($3 <= 20) print  "USERNAME: " $1 " GID: " $3 " UID: " $4}' /etc/passwd
USERNAME: root GID: 0 UID: 0
USERNAME: bin GID: 1 UID: 1
USERNAME: daemon GID: 2 UID: 2
USERNAME: adm GID: 3 UID: 4
USERNAME: lp GID: 4 UID: 7
USERNAME: sync GID: 5 UID: 0
USERNAME: shutdown GID: 6 UID: 0
USERNAME: halt GID: 7 UID: 0
USERNAME: mail GID: 8 UID: 12
USERNAME: news GID: 9 UID: 13
USERNAME: uucp GID: 10 UID: 14
USERNAME: operator GID: 11 UID: 0
USERNAME: games GID: 12 UID: 100
USERNAME: gopher GID: 13 UID: 30
USERNAME: ftp GID: 14 UID: 50

Search the input for the lines that match only a specific field/column (In example given below, its searching lines which has /bin/bash in the last column):

#  awk -F ':' '$NF ~ /\/bin\/bash/' /etc/passwd
root:x:0:0:root:/root:/bin/bash


Simple additions of a specific field (In example given below first we match the lines starting with word “r”, then summarize their value of the 3rd field):

# awk -F ':' '/^r/ {sum += $3} END {print sum}' /etc/passwd
98

Below is the value in 3rd field of each line sytarting with [r]

# awk -F ':' '/^r/ {print $3}' /etc/passwd
0
37
32
29

Matching a specific condition and counting how often it appears (In example given below, its searching for lines which start with letter [s,r] and count such occurrences):

# awk -F ':' 'BEGIN { count=0;} /^r/ { count ++; } END {print "Number of occurrences for line starting with r =",count;}' /etc/passwd
Number of occurrences for line starting with r = 4
# awk -F ':' 'BEGIN { count=0;} /^s/ { count ++; } END {print "Number of occurrences for line starting with r =",count;}' /etc/passwd
Number of occurrences for line starting with r = 6

Confirming the same using grep command

# grep '^[r]' /etc/passwd |wc -l
4
# grep '^[s]' /etc/passwd |wc -l
6

Matching a specific condition for a field and counting how often it appears (In example given below, its searching for lines which has /bin/bash in the last column field and count such occurrences):

# awk -F ':' '$NF ~ /\/bin\/bash/ { n++ }; END { print "Number of occurrences for /bin/bash in last column = ",n }' /etc/passwd
Number of occurrences for /bin/bash in last column =  1

Confirming the same using grep command

# grep '/bin/bash$' /etc/passwd |wc -l
1


References:-
http://www.xenuser.org/simple-awk-cheat-sheet
http://awk.info/?awk1line
http://www.thegeekstuff.com/

Back To Top
Home

No comments:

Post a Comment