How to read a file line by line using awk

by Vivek Gite · 4 comments

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:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 4 comments… read them below or add one }

1 codeguru 02.26.09 at 6:46 am

That was pretty cool. Had problems understanding awk. The tip really enlightened me. Thanks

2 TrueColorTech 06.01.09 at 4:08 pm

Thanks for the tip, made things much easier.

3 sundar 06.26.09 at 2:34 am

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. :-)

4 http://www.eteenterprises.com 01.19.10 at 10:33 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All