Monday, December 14, 2009

Extract Last Field

You can use 'cut' to extract the last field of a line if you know how many fields there are, eg:
field=`cut -d: -f8 file`

But if you don't know the maximum number of fields or the number of fields per line are not consistent, awk can come to the rescue. awk has the inbuilt variable NF for the number of fields. By using this variable we can use it to extract the last field by using:

field=`awk -F: '{print $NF}'`

or you can use calculations to retrieve any field relative to the last field. For example to retrieve the second last field, use:

field=`awk -F: '{print $(NF-1)}'`

Thursday, October 29, 2009

Making current directory available on HTTP

python -m SimpleHTTPServer

Serve current directory tree at http://$HOSTNAME:8000/

COMMANDLINEFU.COM

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Thursday, June 4, 2009

Download a website

Here is a simple effective way to get the files downloaded recursively from a website without actually visiting each and every link to the sub pages.

wget -r -p -k -E http://www.linuxdriver.co.il/ldd3/

...where
  • -r is for recursive download of pages
  • -p is for linking pages locally so that user can browse them easily once the download is completed.
  • -k is to create the directory structure, and
  • -E is to create .html extensions to the type XHTML or text files.

Kill all process instances at one go

Sometimes you might need to urgently kill all the java processes. Use the following command to do so:

ps -ef | grep java | xargs kill -9 awk '{print $2}' > /dev/null 2>&1