Friday, May 27, 2011

sed - Stream Editor

'sed' is a stream editor.  A stream editor is used to perform basic text
transformations on an input stream. It doesn't alter the file contents.

Format is

sed OPTIONS... [SCRIPT] [INPUTFILE...]

sed  's/He/She/' letter.txt

Here 'letter.txt' is a filename where 'He' will be replaced by 'She' and displayed in the screen. 's' is a separator.

'sed' makes a maximum of one change per line

To have multiple changes per line we have to use as follows

sed  's/He/She/g' letter.txt

Also 'sed' searches are case sensitive and hence in-order to make search in-case sensitive we have to use the command as follows

sed  's/He/She/gi' letter.txt

To make many changes on a file, we can use the command as

sed -e  's/He/She/gi' -e  's/handsome/beautiful/gi' letter.txt

To make changes only on particular lines then use

sed '2,6s/He/She/g' letter.txt

Here entire letter.txt will be displayed on the screen but only lines '2' and '6' will be replaced by 'He' to 'She'.

We can also give starting and ending word  and search and replace the word in lines between words

sed  '/Dear/,/waiting/s/He/She/gi' letter.txt

No comments:

Post a Comment

Powered By Blogger

Followers