HostOnNet Blog

awk

Looking for Linux Server Admin or WordPress Expert? We can help.

awk is a command line tool for pattern scanning and processing.

Here are some examples of awk in action.

In this example awk split text using space. Print first part. $0 means full line, $1 first part, $2 second part and so on.

cat /etc/passwd | awk '{print $1}'
root@backup:~# cat /etc/passwd | awk '{print $1}'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-timesync:x:100:102:systemd
systemd-network:x:101:103:systemd
systemd-resolve:x:102:104:systemd
systemd-bus-proxy:x:103:105:systemd
syslog:x:104:108::/home/syslog:/bin/false
_apt:x:105:65534::/nonexistent:/bin/false
lxd:x:106:65534::/var/lib/lxd/:/bin/false
messagebus:x:107:111::/var/run/dbus:/bin/false
uuidd:x:108:112::/run/uuidd:/bin/false
dnsmasq:x:109:65534:dnsmasq,,,:/var/lib/misc:/bin/false
sshd:x:110:65534::/var/run/sshd:/usr/sbin/nologin
boby:x:1000:1000:Yujin
openvpn:x:1001:1001::/home/openvpn:/sbin/nologin
mysql:x:111:118:MySQL
postfix:x:112:119::/var/spool/postfix:/bin/false
mongodb:x:113:65534::/var/lib/mongodb:/bin/false
root@backup:~# 

This is not very useful. What if we need to list all users in a linux system ? We need to tell awk to split the lines using character “:”. This can be done with -F parameter.

cat /etc/passwd | awk -F":" '{ print $1}'

You can also use -F: instead of -F”:”

instead of piping, awk can read directly from the file

awk -F: '{print $1}' /etc/passwd

Posted in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.