Awk by examples

posted by Arkham on April 14th, 2009


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.


  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 {
    { 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);
    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

Tags: , , , ,

Free your memory on Linux

posted by Arkham on December 19th, 2008


No, this is not a howto to make your RAM Stallman-compliant.

Sometimes, it is handy to empty your caches, like when Virtualbox checks the available memory before actually starting. From linux 2.6.16 there are some nice tricks for dropping the memory cache, dentries and inodes.

First of all, we need to free all the cached objects.
Run as root:


Then, to free pagecache:

echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

echo 3 > /proc/sys/vm/drop_caches

Now, check your available memory using free and all your cache are belong to us.. *hu-uhm* ..should have been freed. Freed as in freed-om ;)

Tags: ,

KDE-like bigger icons for Gnome menu

posted by Arkham on October 23rd, 2008

I’ve tried to use some kind of KDE like, bigger icons on my desktop for a while and I realized that it is really a pity to not use the wonderful 32×32 tango icons for the gnome menu also. If you want to try, it’s very simple:

gedit ~/.gtkrc-2.0

and put in this line:

gtk-icon-sizes = "panel-menu=32,32"

and restart gnome-panel (maybe you could be more gracious :P):

killall gnome-panel

Here is the outcome:

Tags: , ,

Colored Man Pages

posted by Arkham on September 25th, 2008

To improve your productivity when you’re reading long pages of fine manuals (aka when you’re doing some healthy RTFM), it is a very nice trick to add some colour in the terminal, so you can jump quickly from section to section or find the name of that sneaky option with a glance…

Add to your ~/.bashrc:

export LESS_TERMCAP_mb=$'E[01;31m'
export LESS_TERMCAP_md=$'E[01;31m'
export LESS_TERMCAP_me=$'E[0m'
export LESS_TERMCAP_se=$'E[0m'
export LESS_TERMCAP_so=$'E[01;44;33m'
export LESS_TERMCAP_ue=$'E[0m'
export LESS_TERMCAP_us=$'E[01;32m'


source ~/.bashrc


Tags: ,

Mplayer and Nautilus “Open with”

posted by Arkham on June 19th, 2008

Nautilus “Open with” option and Mplayer don’t seem to behave correctly when the file we want to open contains spaces. The dirty hack to solve it is:

vi /usr/share/applications/mplayer.desktop

And replace “Exec=gmplayer %U” with “Exec=gmplayer %F”


Tags: , ,