Posts by Category


Pure New Zealand

This site is driven by Blosxom

his site was written in vi

SDF is driven by NetBSD

Subscribe to this sites RSS/XML feed

This site has a Tableless Stylesheet

Email me

Jun 02, 2005

Grep and Sed Tips

I'm terrible at this type of thing so I need lots of examples

Simple 'sed' Example

sed '3,10s/REGEXP/replacement' book

Will search the file 'book' and replace 'REGEXP' with 'replacement' on the third and tenth lines only.

Some more simple examples

sed 's/red/green'

Replaces the first occurrence of 'red' in a line with 'green'

sed 's/red/green/4'

Replaces the fourth occurrence of 'red' in a line with 'green'

sed 'car/!s/red/green/g'

Replaces all instances of 'red' with 'green' except in lines containing the word 'car'

Simple 'grep' Example

Find a matching phone number

grep '408.[0-9]\{3\}.[0-9]\{4\}' mail/*

Will search all files in the mail directory for a pattern which will match 408<'.' = any single character><[0-9]\{3\} = three digit number><'.' = any single character><[0-9]\{4\} = four digit number>

Note the use of '\' to escape (ignore) the following special character '{' and '}'

Permalink | 2005.06.02-03:46.00