Install this theme
awk one-liners

mainly for text processing, reformatting & transformations

  • extract the first column from a file: awk '{print $1}' file

  • renaming files (append ‘.new’): ls files_list | awk '{print "mv "$1" "$1".new"}' | sh

  • renaming within a name: ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh

  • remove only files:

    • ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
    • ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
  • remove only directories:

    • ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
    • ls -p | grep /$ | wk '{print "rm -r "$1}'
    • ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
  • killing processes by name, eg. netscape:

    • kill ps auxww | grep netscape | egrep -v grep | awk '{print $2}'
    • ps auxww | awk '$0~/netscape/&&$0!~/awk/{print $2}' |xargs kill
  • rearranging columns:

    • 1234 HD 13324 22:40:54
    • 1235 HD122235 22:43:12
    • awk ‘{ printf $NF;$NF = “” ;printf ” “$0”\n” }’ | sort
    • 22:40:54 1234 HD 13324
    • 22:43:12 1235 HD122235
  • pretty printing tables (with header & footer)

  awk 'BEGIN {  print "Month Crates"
                print "----- ------" }
             {  print $1, "   ", $2  }
       END   {  print "----- ------" }' testfile
  • where testfile contains:
Jan 13
Feb 15
Mar 15
  • working over a subset of lines instead of all lines in file

    • precede by <condition>
    • examples:
    • print entire line of all lines in hosts file starting with “127.0.0.1”: awk '/^127.0.0.1 missscatterbrain/ {print}' /etc/hosts
    • print line where 7th item contains “bash”: awk -F: '$7 ~ /bash/ {print $1}' /etc/passwd | sort
  • as a programming language

    • implements necessary flow control structures
    • a set of operators
    • predefined functions to deal with numbers & strings
    • user defined functions
    • able to manage common scalar vars & variable sized arrays
    • libraries exist: gawk