SED Simplified

 Apr, 11 - 2017   no comments   CentosLinuxUbuntu

SED was early developed in 1974 by Lee E. McMahon, it is an acronym that stands for “Steam EDitor”, it is simply do the following operations on the input file that your are specifying.

 

Sed has the following formula:

$ sed OPTIONS COMMAND INPUT

Also, It could be used in pipelines too:

$ cat INPUTFILE | sed OPTIONS COMMAND

 

Options:

-ewhich stands for expression or may be editing expression and used to specify a command to be executed.

-f stands for file and used in case of reading commands from a file to be executed by sed.

-rthis option can be used to use extended regular expressions instead of the basic regular expression

-i used to edit file in place, it saves a backup of the original file

-n will be silent and quiet while working (by default sed is printing each line from buffer to process but when using this option it will work silently without printing it)

Sed do the following algorithm that executes for each line in the file or line by line:

Repeat
1. Read Line and put it in an pattern buffer
2. Execute the sed command that is in-between commas
3. Display the results on output stream (standard output by default, file, or any other resource)
Until end of file

To understand this concept let’s apply this on a text input file, create a file named test.txtas below:

$ echo "this is line 1
this is line 2
this is line 3
this is line 4" > test.txt

If you just run the following command, it will simply read each line into buffer, execute sed command on it (no command at all) and finally print the results out to standard output, which means it will print out the whole content of the file (exactly as catdoes)

$ sed '' test.txt

In the following example, SED will accept user input until he/she presses Ctrl+D

 $ sed ''

let’s dive into expressions or commands that can be used with examples:

Delete specific line number from a file use the formula ${line_number}d, the following example should delete first and second line:

sed -e '1d' -e '2d' test.txt

Note: this will not change the source file itself, remember step 3 in its algorithm  Display the results on output stream (either standard output, file, or any other resource)

The above command deletes first and second lines and results back the remaining to the standard output as the following:

this is line 3
this is line 4

You can put the commands into a file and use that file as an input to sed, suppose we have a file named “commands.txt” which has the content “1d\n2d” then the following example will delete the first and second lines from test.txt

sed -f commands.txt test.txt

 

Printing commands:

The following will duplicate each line twice and prints out the results back to standard output stream:

sed -e 'p' test.txt

The result should be as below:

this is line 1
this is line 1
this is line 2
this is line 2
this is line 3
this is line 3
this is line 4
this is line 4

But you might wonder why each line is being printed twice. Let us find out. do you still remember the workflow or the algorithm of SED? By default, SED prints the contents of the pattern buffer. In addition, I have included a print command explicitly in our command section. Hence each line is printed twice.

Do you remember -n option can be used to suppress the default printing of the pattern buffer so we might use the following script to only print each line once:

sed -n 'p' test.txt

If you want to print 3rd line you can do this by sed -n '3p' test.txt which will print out:

this is line 3

if you want to print lines from 1 to 3, use the following: sed -n '1,3 p' test.txt which prints out:

this is line 1
this is line 2
this is line 3

If you want to print the last line you can use sed -n '$ p' test.txt which prints out:

this is line 4

If you want to print from 2nd line to the end of file, use sed -n '2,$ p' test.txtthat should result in:

this is line 2
this is line 3
this is line 4

 


Related articles