Tuesday, October 2, 2007

Unix Command Samples - awk

Remove first 3 lines in a file:
$cat bearish.htm | awk 'NR>3'

Rounding Number:
Need to be able to round numbers in bash to the nearest whole number? You can use a single line of code in AWK to do the job. For example, the following code will make the number 29.498398 round up to 29.50:

$ echo 29.498398 | awk '{ rounded = sprintf("%.2f", $1); print rounded }'
29.50

Unix Command Samples - Sed

Display file without the blank lines and remove the first line:
$ cat bearish.htm | sed -e '/^ *$/d;1,1d'

Remove first 3 lines in a file:
$cat bearish.htm | sed -e '1,3d'

Remove HTML tags:
$cat bearish.htm | sed -e 's/<[^>]*>//g'