You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
6.3.6. The printf program<br />
For more precise control over the output format than what is normally provided by print, use printf. The<br />
printf command can be used to specify the field width to use for each item, as well as various formatting<br />
choices for numbers (such as what output base to use, whether to print an exponent, whether to print a sign,<br />
and how many digits to print after the decimal point). This is done by supplying a string, called the format<br />
string, that controls how and where to print the other arguments.<br />
The syntax is the same as for the C-language printf statement; see your C introduction guide. The gawk info<br />
pages contain full explanations.<br />
6.4. Summary<br />
The gawk utility interprets a special-purpose programming language, handling simple data-reformatting jobs<br />
with just a few lines of code. It is the free version of the general UNIX awk command.<br />
This tools reads lines of input data and can easily recognize columned output. The print program is the most<br />
common for filtering and formatting defined fields.<br />
On-the-fly variable declaration is straightforward and allows for simple calculation of sums, statistics and<br />
other operations on the processed input stream. Variables and commands can be put in awk scripts for<br />
background processing.<br />
Other things you should know about awk:<br />
• The language remains well-known on UNIX and alikes, but for executing similar tasks, Perl is now<br />
more commonly used. However, awk has a much steeper learning curve (meaning that you learn a lot<br />
in a very short time). In other words, Perl is more difficult to learn.<br />
• Both Perl and awk share the reputation of being incomprehensible, even to the actual authors of the<br />
programs that use these languages. So document your code!<br />
6.5. Exercises<br />
These are some practical examples where awk can be useful.<br />
1.<br />
For the first exercise, your input is lines in the following form:<br />
Username:Firstname:Lastname:Telephone number<br />
Make an awk script that will convert such a line to an LDAP record in this format:<br />
dn: uid=Username, dc=example, dc=com<br />
cn: Firstname Lastname<br />
sn: Lastname<br />
telephoneNumber: Telephone number<br />
Create a file containing a couple of test records and check.<br />
2. Create a <strong>Bash</strong> script using awk and standard UNIX commands that will show the top three users of<br />
disk space in the /home file system (if you don't have the directory holding the homes on a separate<br />
partition, make the script for the / partition; this is present on every UNIX system). First, execute the<br />
commands from the command line. Then put them in a script. The script should create sensible output<br />
(sensible as in readable by the boss). If everything proves to work, have the script email its results to<br />
you (use for instance mail -s Disk space usage < result).<br />
Chapter 6. The GNU awk programming language 77