воскресенье, 4 сентября 2011 г.

Awk Tips

Отлична штука этот awk! Вот сами смотрите!



  • 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)




  • 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!






  • 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!






  • 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

    It has to be adjusted to fit the ps command on whatever unix system you are on. Basically it is: "If the process is called netscape and it is not called 'grep netscape' (or awk) then print the pid"





  • Источник: http://www.linuxfocus.org/English/September1999/article103.html

    Для познания дао смотри тут

    3 комментария:

    1. Омайгад :) awk, конечно крут, но всё-таки не стоит так делать :)

      1) mv $x `sed 's/old/new/g' <<< $x`
      2) find -type f -delete
      3) find -type d -delete
      4) killall?

      ОтветитьУдалить
    2. Почему не стоит? :)

      Это просто примеры для демонстрации возможностей. Чтобы заинтересовать! :)

      ОтветитьУдалить