Monday, November 17, 2008

Shell regular expressions

Sedatives with sed

sed -i -e 's/template/supplement/g'

-e allows to chain more rules for replace
/g replaces all occurences (global)
-i edit current file in place (use -ibackup for backup)

Fresh grepfruit

grep -P 'template' file

prints lines from file which match regex template.
-P uses Perl-compatible regular expressions (if not available, try -E instead)
-E uses extended regular expressions

Awkward tasks

who | awk -F ' ' '{print $1}'

prints first item of each line.
-F specifies item delimiter, default is white character.

No comments: