Processing files with awk, part two

Unix Insider |  Operating Systems

One more piece of awk syntax will make it an even more useful tool. I
said in last month's column that awk treats the spaces in a record
as a field separator. It is possible to change the field separator
to another value.

Figure 1 is an example of a passwd file. The password itself
in this example is replaced with a single exclamation mark.
This file has several separate fields in it, but the field
separator is a colon (:) rather than spaces.

Figure 1

root:!:0:1:Super User:/:
daemon:!:1:1:System Daemons:/etc
lbw:!:209:200:Lavinia Bowder Washinton:/home/lbw:/bin/csh
bob:!:210:200:Robbie Cramer:/home/bob:/bin/ksh
joann:!:213:200:Jo Ann Batson:/home/joann:/bin/ksh
jlan:!:214:200:Jack Landon:/home/jlan:/bin/ksh
jank:!:215:200:Jan Kingly:/home/jank:/bin/ksh
ljn:!:216:200:Laura Nugent:/home/ljn:/bin/ksh
mjb:!:220:200:Mo Budlong:/home/mjb:/bin/ksh
bda:!:235:500:Basic Development Accnt:/home/bda:/bin/ksh
guest1:!:501:500:Guest1 Account:/disk2/guest1:/bin/ksh
guest2:!:502:500:Guest2 Account:/disk2/guest2:/bin/ksh
guest3:!:503:500:Guest3 Account:/disk2/guest3:/bin/ksh
beb:!:248:202:Becky E Brown  :/home/beb:/bin/ksh

A passwd file can be used as the input file to awk for for
an awk report by changing the field separator. Figure 2 is a
short example. There are two points to notice.

First the logic in BEGIN{FS=":"}. In awk, FS is a
pre-defined variable that contains the field separator. If
you make no changes to it, the FS value is set to spaces. In
this listing, the BEGIN logic sets FS to a colon (:), so the
value of the field separator is changed before the first
record is read. This allows the passwd records to be broken
into fields at the colons.

The second point to notice is on line 3 of Figure 2. In all
previous examples the file has been piped into awk using
"ls -l|awk etc." In this example, the file is specifically
named by placing it on the command line after the closing
single quote at the end of the awk commands. Awk can take
its input from a pipe as in previous examples, or from an
explicitly named file (or files) as in Figure 2.
Remember that the closing quote ends multiline input so be
sure to type the closing quote, a space and then the name of
the file.

Remember to type a TAB wherever you see the ^ mark.

Figure 2

awk '
{print $1 "  ^" $5}' /etc/passwd

Unless you are in the C shell, the closing quote ends
multiline input so be sure to type the closing quote,
followed by a space and followed by the name of the file.

Figure 3 is an example using C shell continuation characters.
The example shown in Figure 2 works correctly. Figure 4
gives you two further examples, one version that won't work and
another that will.

Join us:






Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question