Wednesday, June 9, 2010

Bourne shell redirection

0 is stdin;
1 is stdout;
2 is stderr


redirect std err to file
$command 2>file

direct the output from command to stderr
$commamd >& 2

direct std output and std err to a file or /dev/null
$ command >file.log >>file.log
$ command >file.log 2>&1
$ command >/dev/null 2>&1

Some cool one liners..

sum numbers in first column of a file
$ cat filename | awk 'BEGIN { tot=0 } { tot+=$1 } END { print tot }'

only print certain lines in a file, eq 20 - 30
$ sed -n -e 20,30p filename

Search all files in and below for a string xyz:
$ find . -exec egrep xyz {} \; -print

Remove all files name ".log.tmp"
$ find . -name .log.tmp -exec rm {} \;

search a file for any of three strings:
$ egrep 'abc|xyz|or' filename