Q. How do I read a file line by line using awk utility?
A. awk is pattern scanning and text processing language. It is useful for manipulation of data files, text retrieval and processing, and for prototyping and experimenting with algorithms. The name AWK is derived from the surnames of its authors — Alfred Aho, Peter Weinberger, and Brian Kernighan.
By default it process one line at a time. For example following command will simply process one line at a time:
$ cat /etc/passwd | awk '{ print $0}'
Or better try
$ awk '{ print $0}' /etc/passwd
print command is used to output text. $0 is field name for entire line.
By default white space (blank line) act as filed separator. You can set new filed separator with -F option. For example use : as filed separator:
$ awk -F':' '{ print $1 }' /etc/passwd
Above command will print all username using first field ($1) for current line. You can print username ($1), shell ($7) and login home dir ($6) report as follows:
$ awk -F':' '{ print "User " $1 " login using " $7 " shell with as " $6 " home dir"}' /etc/passwd
Output:
User root login using /bin/bash shell with as /root home dir User daemon login using /bin/sh shell with as /usr/sbin home dir User bin login using /bin/sh shell with as /bin home dir User sys login using /bin/sh shell with as /dev home dir .... .... User gdm login using /bin/false shell with as /var/lib/gdm home dir User vivek login using /bin/bash shell with as /home/vivek home dir User sshd login using /usr/sbin/nologin shell with as /var/run/sshd home dir
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- My 10 UNIX Command Line Mistakes
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email FAQ to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: 02/21/07



{ 4 comments… read them below or add one }
That was pretty cool. Had problems understanding awk. The tip really enlightened me. Thanks
Thanks for the tip, made things much easier.
That’s a good tip! By the way, how to read a file, compare a field value then insert record in between using awk? Can you help? Thanks. :-)
Hi great article. One suggestion is that the file names in the awk scripts can be substituted for the standard input.
e.g. to get the number of lines in a file you could use
wc -l filename | awk ‘{ print $1 }’
Andre