Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

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.

 
 
  Published under the terms of the GNU General Public License Design by Interspire