Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
The field separator is represented by the built-in variable FS. Note that this is something different from the<br />
IFS variable used by POSIX-compliant shells.<br />
The value of the field separator variable can be changed in the awk program with the assignment operator =.<br />
Often the right time to do this is at the beginning of execution before any input has been processed, so that the<br />
very first record is read with the proper separator. To do this, use the special BEGIN pattern.<br />
In the example below, we build a command that displays all the users on your system with a description:<br />
kelly is in ~> awk 'BEGIN { FS=":" } { print $1 "\t" $5 }' /etc/passwd<br />
--output omitted--<br />
kelly Kelly Smith<br />
franky Franky B.<br />
eddy Eddy White<br />
willy William Black<br />
cathy Catherine the Great<br />
sandy Sandy Li Wong<br />
kelly is in ~><br />
In an awk script, it would look like this:<br />
kelly is in ~> cat printnames.awk<br />
BEGIN { FS=":" }<br />
{ print $1 "\t" $5 }<br />
kelly is in ~> awk -f printnames.awk /etc/passwd<br />
--output omitted--<br />
Choose input field separators carefully to prevent problems. An example to illustrate this: say you get input in<br />
the form of lines that look like this:<br />
"Sandy L. Wong, 64 Zoo St., Antwerp, 2000X"<br />
You write a command line or a script, which prints out the name of the person in that record:<br />
awk 'BEGIN { FS="," } { print $1, $2, $3 }' inputfile<br />
But a person might have a PhD, and it might be written like this:<br />
"Sandy L. Wong, PhD, 64 Zoo St., Antwerp, 2000X"<br />
Your awk will give the wrong output for this line. If needed, use an extra awk or sed to uniform data input<br />
formats.<br />
The default input field separator is one or more whitespaces or tabs.<br />
6.3.2. The output separators<br />
6.3.2.1. The output field separator<br />
Fields are normally separated by spaces in the output. This becomes apparent when you use the correct syntax<br />
for the print command, where arguments are separated by commas:<br />
kelly@octarine ~/test> cat test<br />
Chapter 6. The GNU awk programming language 74