Saturday, February 13, 2010

About AWK Command in Unix

  1. Renaming within the name:
    ls -1 *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh
    (although in some cases it will fail, as in file_old_and_old)
  2. remove only files:
    ls -l * | grep -v drwx | awk '{print "rm "$9}' | sh
    or with awk alone:
    ls -l|awk '$1!~/^drwx/{print $9}'|xargs rm
    Be careful when trying this out in your home directory. We remove files!
  3. remove only directories
    ls -l | grep '^d' | awk '{print "rm -r "$9}' | sh
    or
    ls -p | grep /$ | wk '{print "rm -r "$1}'
    or with awk alone:
    ls -l|awk '$1~/^d.*x/{print $9}'|xargs rm -r
    Be careful when trying this out in your home directory. We remove things!
  4. killing processes by name (in this example we kill the process called netscape):
    kill `ps auxww | grep netscape | egrep -v grep | awk '{print $2}'`
    or with awk alone:
    ps auxww | awk '$0~/netscape/&&$0!~/awk/{print $2}' |xargs kill 

1 comment: