Linux Know-How provides a collection of introductory texts on often needed Linux skills.


Exercises - Gawk

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     looooooooooooooooooooooong line, but i mean really looooooooooooooooooooooong.
    
    

    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>
    looooooooooooooooooooooong line, but i mean really looooooooooooooooooooooong.
    </entry>
    </row>
    

    Additionally, if you know anything about XML, write a BEGIN and END script to complete the table. Or do it in HTML.


Last Update: 2010-12-16