Sunday, April 22, 2012

Linux / Unix AWK: Read a Text File

How do I read a text file using awk pattern scanning and text processing language under Linux / Unix like operating systems?

The default syntax to read a text file line-by-line using awk is as follows:
 ### note '{ print }' '{ print $0 }' '1' all are same ###
awk '{ print }' /path/to/file
awk '{}1' /path/to/file
awk '1' /path/to/file
awk '{ print $0 }' /path/to/file
awk '{}1' /etc/passwd
awk '{print $0}' /etc/hosts
awk '{print}' /etc/hosts
 
Sample outputs:
127.0.0.1 localhost
192.168.1.5 wks01
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.1.10 nas01
192.168.1.11 nas02
192.168.1.12 nas03
192.168.2.50 nfs2.vmware.nixcraft.net.in nfs2v
192.168.2.51 nfs1.vmware.nixcraft.net.in nfs1v
172.168.232.50 nfs1.kvm.nixcraft.net.in nfs1k
172.168.232.51 nfs2.kvm.nixcraft.net.in nfs2k
The print command is used to display text on screen. The simplest form of this command:
 
awk '{ print }' filename
 
This displays the contents of the current record. However, in AWK, records are broken down into fields, and these can be displayed separately. To see 1st field, enter:
 
awk '{ print $1 }' filename
 
To see first and fourth fields of the current record, enter:
 
awk '{ print $1, $3 }' filename
 
You can set the output field separator (OFS) whose default value is a single space character using the -F option as follows:
### set OFS to : ###
### print user name, home directory and shell ###
awk -F: '{ print $1 " users home directory set to \"" $6 "\" with " $7 " as login shell" }' /etc/passwd
 
Sample outputs:
root users home directory set to "/root" with /bin/bash as login shell
daemon users home directory set to "/usr/sbin" with /bin/sh as login shell
bin users home directory set to "/bin" with /bin/sh as login shell
sys users home directory set to "/dev" with /bin/sh as login shell
sync users home directory set to "/bin" with /bin/sync as login shell
games users home directory set to "/usr/games" with /bin/sh as login shell
man users home directory set to "/var/cache/man" with /bin/sh as login shell
lp users home directory set to "/var/spool/lpd" with /bin/sh as login shell
mail users home directory set to "/var/mail" with /bin/sh as login shell
news users home directory set to "/var/spool/news" with /bin/sh as login shell
uucp users home directory set to "/var/spool/uucp" with /bin/sh as login shell
proxy users home directory set to "/bin" with /bin/sh as login shell
www-data users home directory set to "/var/www" with /bin/sh as login shell
backup users home directory set to "/var/backups" with /bin/sh as login shell
list users home directory set to "/var/list" with /bin/sh as login shell
irc users home directory set to "/var/run/ircd" with /bin/sh as login shell
gnats users home directory set to "/var/lib/gnats" with /bin/sh as login shell
nobody users home directory set to "/nonexistent" with /bin/sh as login shell
libuuid users home directory set to "/var/lib/libuuid" with /bin/sh as login shell
messagebus users home directory set to "/var/run/dbus" with /bin/false as login shell
Debian-exim users home directory set to "/var/spool/exim4" with /bin/false as login shell
statd users home directory set to "/var/lib/nfs" with /bin/false as login shell
avahi-autoipd users home directory set to "/var/lib/avahi-autoipd" with /bin/false as login shell
avahi users home directory set to "/var/run/avahi-daemon" with /bin/false as login shell
usbmux users home directory set to "/home/usbmux" with /bin/false as login shell
Debian-gdm users home directory set to "/var/lib/gdm3" with /bin/false as login shell
saned users home directory set to "/home/saned" with /bin/false as login shell
hplip users home directory set to "/var/run/hplip" with /bin/false as login shell
vivek users home directory set to "/home/vivek" with /bin/bash as login shell
bind users home directory set to "/var/cache/bind" with /bin/false as login shell
haldaemon users home directory set to "/var/run/hald" with /bin/false as login shell
sshd users home directory set to "/var/run/sshd" with /usr/sbin/nologin as login shell
mysql users home directory set to "/var/lib/mysql" with /bin/false as login shell
radvd users home directory set to "/var/run/radvd" with /bin/false as login shell
The commands "print" and "print $0" are identical in functionality.

Conditional awk printing

The awk syntax is as follows:
 
awk 'condition { action }' filename
awk '1 == /match/ { action }' filename
awk '1 == /match/ { print $0 }' filename
awk 'pattern { action }' filename
 
If first field match to vivek print the entire line:
$ awk -F: '1 == /vivek/ { print $0}' /etc/passwd
OR,
$ awk -F: '/vivek/ { print $0}' /etc/passwd
For more information go through awk man page:
$ man awk

No comments:

Post a Comment