Linux FAQ's & Manuals

6.5. exercises

these are some practical examples where awk can be useful.

  1. for the first exercise, your input is lines in the following form:

    username:firstname:lastname:telephone number

    make an awk script that will convert such a line to an ldap record in this format:

    
dn: uid=username, dc=example, dc=com cn: firstname lastname sn: lastname telephonenumber: telephone number 

    create a file containing a couple of test records and check.

  2. create a bash script using awk and standard unix commands that will show the top three users of disk space in the /home file system (if you don't have the directory holding the homes on a separate partition, make the script for the / partition; this is present on every unix system). first, execute the commands from the command line. then put them in a script. the script should create sensible output (sensible as in readable by the boss). if everything proves to work, have the script email its results to you (use for instance mail -s disk space usage < result).

    if the quota daemon is running, use that information; if not, use find.

  3. create xml-style output from a tab-separated list in the following form:

    
meaning very long line with a lot of description   meaning another long line   othermeaning    more longline   testmeaning     looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line, but i mean really looooooooooooooooooooooooooooooooooooooooooooooooooong.   

    the output should read:

    
<row> <entry>meaning</entry> <entry> very long line </entry> </row> <row> <entry>meaning</entry> <entry> long line </entry> </row> <row> <entryothermeaning</entry> <entry> more longline </entry> </row> <row> <entrytestmeaning</entry> <entry> looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line, but i mean really looooooooooooooooooooooooooooooooooooooooooooooooooong. </entry> </row> 

    additionally, if you know anything about xml, write a begin and end script to complete the table. or do it in html.