sed stand for Stream EDitor. It is more to ascii text manipulation, but this time while it’s from the command line instead of in the text editor like vi. The replace command looks exactly as the vi editor replace command.

Normally I would use it with the pipe (|) to filter the output of the previous executed command. For example, I want to capture the output of my file listing into a file for training purposes. I would do something like this:

$ ls -la > out1.txt
$ cat out1.txt
total 24
drwx------ 2 rosley users 4096 Feb 21 14:37 ./
drwx-----x 14 rosley users 4096 Feb 21 14:37 ../
-rw------- 1 rosley users 36 Feb 21 14:36 file1.txt
-rw------- 1 rosley users 60 Feb 21 14:37 file2.txt
-rw------- 1 rosley users 144 Feb 21 14:37 file3.txt
-rw------- 1 rosley users 96 Feb 21 14:37 file4.txt

But I don’t want to publish my username in the output listing, I would like to if possible replace it with some other username. I have 2 choices, 1st is the typical way, use the find and replace function in the vi editor or 2nd choice is by straight away filter it out during the capturing process.

$ ls -la | sed 's/rosley/user1/' > out1.txt
$ cat out1.txt
total 24
drwx------ 2 user1 users 4096 Feb 21 14:37 ./
drwx-----x 14 user1 users 4096 Feb 21 14:37 ../
-rw------- 1 user1 users 36 Feb 21 14:36 file1.txt
-rw------- 1 user1 users 60 Feb 21 14:37 file2.txt
-rw------- 1 user1 users 144 Feb 21 14:37 file3.txt
-rw------- 1 user1 users 96 Feb 21 14:37 file4.txt

This is how the above process works, first it will run the ls -al command, then the pipe (|) will cause the output of earlier command (ls -al) been transferred as an input for the next command which is the sed command.

In this example, we use sed to find the word rosley in every line and replace it with user1, and finally redirect the output into out1.txt file. Now lets take a look at the sed option that we use in the above example: ‘s/rosley/user1/’. The s stand for substitute, the slash will separate the string that we want to find and the replacement string.

The ‘s/string1/string2/’ will only replace the first string that matches the string1 in every line, if there are more than one appearance of string1 in the line, only the first one will be replaced with string2. If it is your intention to replace all the string1 with string2, then you should use the global option, like this: ‘s/string1/string2/g’. This will replace all the string1 with string2 indefinitely.

Another option available are the -e which allow us to have multiple find and replace capability in single command. Example below shows how we can replace the owner column (from rosley to user1) and the group column (from users to staff).

$ ls -la | sed -e 's/rosley/user1/'  -e 's/users/staff/g' > out1.txt
$ cat out1.txt
total 24
drwx------    2 user1   staff        4096 Feb 21 14:37 ./
drwx-----x   14 user1   staff        4096 Feb 21 14:37 ../
-rw-------    1 user1   staff          36 Feb 21 14:36 file1.txt
-rw-------    1 user1   staff          60 Feb 21 14:37 file2.txt
-rw-------    1 user1   staff         144 Feb 21 14:37 file3.txt
-rw-------    1 user1   staff          96 Feb 21 14:37 file4.txt

Later we will use it with the awk command.

Tags: , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>