Awk by examples

Posted by Arkham on April 14th, 2009

auk

AWK is a language for processing files of text. A file is treated as a sequence of records, and by default each line is a record. Each line is broken up into a sequence of fields, so we can think of the first word in a line as the first field, the second word as the second field, and so on. An AWK program is of a sequence of pattern-action statements. AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed.

Basics

  1. Print /etc/mtab (awk reads a line at time and prints it) :

    awk '{ print }' /etc/mtab
  2. Print /etc/mtab ($0 denotes the whole line) :

    awk '{ print $0 }' /etc/mtab
  3. List mounted filesystems ($1 denotes the first element of the line) :

    awk '{ print $1 }' /etc/mtab
  4. List groups (-F chooses the Field Separator) :

    awk -F":" '{ print $1 }' /etc/group
  5. List groups and id (awk concatenates print() arguments) :

    awk -F":" '{ print $1 " " $3 }' /etc/group
  6. List groups and id (Nicer format) :

    awk -F":" '{ print "group: " $1 "\tid: " $3 }' /etc/group
  7. Launching external awk scripts :

    # first.awk
    BEGIN {
        FS=":"
    }
    { print $1 }
    awk -f first.awk /etc/mtab
  8. List IPv4 addressess :

    ifconfig | awk '/inet / { print $2 }'
  9. List processes run by root :

    # psroot.awk
    $1 == "root" { 
        printf("ROOT: "); 
        for (i=11; i<=NF; i++) 
            printf("%s ", $i);
        printf("\n")
    }
    ps au | awk -f psroot.awk
  10. Print X warnings :

    awk '$1 ~ /(WW)/ { print }' /var/log/Xorg.0.log
  11. Print a file removing comments :

    awk '! /^#/ { print }' /etc/fstab
  12. Print number of files/directories :

    ls -lA |  awk 'BEGIN{ x=0 } { x=x+1 } END{ print x-1 }'
  13. Awk as calculator :

    echo | awk '{ print ((2*5^2-1)%7) }'
  14. Count empty lines :

    # blanklines.awk
    BEGIN { x=0 } 
    /^$/  { x=x+1 } 
    END   { print "I found " x " blank lines :)" }
    awk -f blanklines.awk /etc/profile
  15. Use regexp as FS (note the difference though) :

    echo '   a   b c  d '| awk -F"[ \t\n]+" '{ print $2 }'
    echo '   a   b c  d '| awk -F" " '{ print $2 }'
  16. Understand NR and NF :

    # nrnf.awk
    BEGIN { x=0 } 
          { print "Words on line " NR ": " NF ; x+=NF } 
    END   { print "Total lines: " NR ; print "Total words: " x }
    awk -f nrnf.awk nrnf.awk

This entry was posted on Tuesday, April 14th, 2009 at 15:27 and is filed under computers. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Tags: , , , ,

3 Responses to “Awk by examples”

  1. Mat_Jack1 Says:

    leetness :P

  2. steffoz Says:

    ma è un post divulgativo o che?

  3. Arkham Says:

    @steffoz: Note to self.