Friday, March 31, 2006

Example of using Sed

SED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ED), SED works by making only one pass over the input(s), and is consequently more efficient. But it is SED's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

One of sed's most useful commands is the substitution command. Using it, we can replace a particular string or matched regular expression with another string. For example say we have a file (called test.txt) with the following content:

aaa bbb ccc

eee fff ggg

bbb ccc aaa

eee rrr kkk

We want to change all the instances of “aaa” to “zzz”. The following sed command could be used to perform the search and replace:

sed -e 's/aaa/zzz/g' test.txt > out.txt

The contents of the output file (out.txt) now has the following:

zzz bbb ccc

eee fff ggg

bbb ccc zzz

eee rrr kkk

Labels:

2 Comments:

Blogger Doug said...

You have your name called Javaman, how come all the posts are about UNIX and ORACLE. lol

11:31 AM  
Blogger Java Man said...

I used java man becase unix man and oracle man were already taken. I actually picked the user name java man because I like coffee and not for the programming language.

2:51 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home