Saturday, September 18, 2010

sed - an introduction

A situation araises in which the admin is supposed to make changes to a particular file in a large number of servers, say about 500 servers. opening and editing the file manually takes ages and it doesn't make any sense particularly when there are tools available such things.

One such powerful tool is 'sed'. sed stands for Stream Editor and it ships with almost all unix flavours. It requires very minimal reaources to run. But it is rarely used as a common editor because it has a very difficult interface.

sed reads its input from standard input one line at a time.
sed uses its editing command on the input stream
sed sends the o/p to the standard output and can be redirected

Lets see with an example

Server1:/home/Aaron# cat file_1
line 1 This is a test file
line 2 We will use this file to test sed
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

Server1:/home/Aaron# sed -e 's/line/LINE/g' file_1
LINE 1 This is a test file
LINE 2 We will use this file to test sed
LINE 3
LINE 4
LINE 5
LINE 6
LINE 7
LINE 8
LINE 9
LINE 10
Server1:/home/Aaron#

This is what has happened:
sed reads the standard input into the pattern space, performs a sequence of editing commands(here substitution of line with LINE) on the pattern space, then writes the pattern space to STDOUT.
Note: The original file is unharmed.

There are a lot of such commands associated with sed. It is one of the powerful utility. Lets have a look at the most frequently used commands.

Lets see more about the commands later.

No comments:

Post a Comment