Saturday, 27 September 2014

SED Command Basics

SED also known as Stream Editor, Here we will discuss the basic functionality of the sed that will help in simple scripts with bash.

General Syntax:


in the above syntax,

OPTIONS: We generally use some options with commands. Options also applies with sed commands. List of sed options can be found in the man page of the sed.

ADDRESS: address here signifies to the location where our operation will apply. There are two types of addressing techniques available.
1. content addressing
2. line addressing

ACTION: action is the operation that we want to apply on the selected lines using address.

FILENAME: it is the path of the file that we want to manipulate with sed.


1. # sed '3,q' filename.txt

In the example 1 here, 3 is the line no and q is the operation applied. It means that from the filename.txt 3rd line will be printed and the quit operation will result in closing the file.

2. # sed -n '1,2p' filename.txt

In the example 2 here, 1 and 2 is the line no and p is the operation. p will print the 1st and 2nd line. Option -n will ensure printing of result only once. If -n option is not used, the text will be printed twice.

3. # sed -n '$p' filename.txt

In the example above, we use $ to print last line of the file. The $ will always used to refer to the end of the file or end of the line.

4. # sed -n '9,11p' filename.txt

We can also refer a range of lines from a file. here we have given the range from 9 to 11 lines. Sed allows to select lines from anywhere in the file.

5. # sed -n '1,4p'
> '7,10p'
> '12,15p' filename.txt

We can select the multiple range groups in one command and apply different operations in the single command.

6. # sed -n -e '1,4p' -e '7,10p' -e '12,15p' filename.txt

If we don't follow the example 5, we can also use the example 6. We can use -e option to specify the different ranges in the address.

7. # sed -n '/echo/p' filename.txt

In the above example we used content addressing. Sed will select the line that has echo in the line and print that line.

8. # sed -n '/echo/, /printf/, /print/p' file.txt

This is the multiple addressing in content addressing.

We have following operation available in the SED command.
1. Inserting Text (i)
2. Deleting text (d)
3. Substitution (s)
4. Writing lines (w)
5. Append Text (a)

9. # sed -n '/echo/w newfile' filename.txt

The lines in the filename.txt, having echo will be copied to newfile and -n option will stop from printing.

10. # sed -n '/echo/w echo.txt,
> '/print/w print.txt' filename.txt

This will write line from filename.txt, the lines having echo will copied to echo.txt and lines having print will be copied to print.txt.

11. # sed -n '1,50w file1.txt
> '51,$w file2.txt' original.txt

This will divide a file original.txt into two files file1.txt and file2.txt, first file will have 50 lines and second file will have rest of files.

12. # sed '1i\
> first line goes here\
> second line goes here\
> third line goes here
> ' original.txt > tmpfile.txt

This will insert lines beginning from first line in the temp file. then we can move the tmp file into original file.

13. # sed '/echo/d' emp.list > newfile.txt

The above example will delete the lines having echo and create a new file with the changes. We can move the new file with original filename.

14. # sed '/^[ /t]*$/d' originalfile.txt > tmpfile.txt

This will delete all the empty lines from original file and create a tempfile that have no empty lines.


SED allows a useful utility in subtitution of strings.
Below example will help to understand these operations.

Basic syntax:

# sed [address] s/exp1/exp2/flags filename.txt

15. # sed 's/echo/printf/' filename.txt > newfile.txt

this will change the echo with printf in the beginning of the file filename.txt and save the changes to newfile.txt. Since the above command don't specify the address, hence applied on the complete file. We can use g flag to change all the occurrences.

16. # sed 's/echo/printf/g' filename.txt > newfile.txt

change all the existance of the echo with printf.

17. #sed '1,5s/echo/printf/g' filename.txt > newfile.txt

This command will look for occurences of echo in first five lines, any echo found here will be changed to printf. Echo after these lines will not be changed.

18. # sed 's/^/#/' file.txt

19. # sed 's/$/#/' file.txt

The above examples will be used to remove the # sign from beginning and the end of the lines.

No comments:

Post a Comment