How to Use SED Command in UNIX with Examples?

Basic SED commands in UNIX
What is SED ?

The SED command in UNIX stands for stream editor, which is used to make changes to file content. In Linux we can simply call it as text editor operator. We will discuss, how to search the strings in a file, update the content of the file, removing the content from the file and replacing the strings in the file.

The workflow of SED command in UNIX
  • Read a line from input stream
  • Execute the commands on a line
  • Display result on output stream
Basic "Options" for SED Command in UNIX
Options           Functionality
          -i            source file can be edited
         -e            To run multiple sed commands in a single sed command
         -n           Suppresses default output
         -f          Precedes a sed script filename
Few basic flags for SED Command in UNIX
Flags                    Functionality
g                         Global replacement of a string in file
i                         Ignore case sensitive with substitution
p                       prints the replaced line on the terminal
w                      Writes lines out to a file
x                      Exchanges contents of the holding buffer with the pattern space
y                     Convert the lower case letters to upper case letters

Some special characters of SED command

Character                   Description

^                    Matches the begining of lines

$                    Matches the end of line

.                     Matches any single character

*                    Matches zero or more occurrences of the previous character

Let us take a file called "sample.txt" and work with different examples
cat sample.txt

Unix is a great OS. Unix is opensource.
The Unix is free OS. Learn operating system.
Linux or Unix which on you choose.

We can find and replace strings or patterns without opening a file. If we are working with large files which can take a long time to open in Vi editor and take a time to replace existing strings with new strings.We can easily replace the strings in a file using the following SED command without opening file.

sed 's/unix/linux/' sample.txt

In the above SED command, first, it performs the search operation for Unix string and replaces with ‘Linux’ for the first occurrence only.Here the “s” specifies the substitution operation. The “/” are delimiters.

By default, the sed command replaces the first occurrence of a pattern in each and it won’t replace the second, third..occurrence in the line.

If we use the ‘g’ flag along with the above command then SED command will replace all unix string with linux.

sed 's/unix/linux/g' sample.txt
SED command with ‘d’ flag used to delete the lines from the files.

1. SED command to delete the First line from the file,

sed '1d' sample.txt

2. SED command to delete the Last line from the file

sed '$d' sample.txt

3. SED command to delete the lines from 1,3 in a file

sed '1,3d' sample.txt

4. SED command to remove blank lines from a file

sed '/^$/d' sample.txt

Here “^” symbol represents the starting point of a line and “$” represents end of the line. Whereas “^$” represents the empty lines.

Following command with 'p' flag to print output to the console

5. We can also print the content of a file using the SED command.

sed -n '10,20p' filename.txt

Using the above command we can filter the content of a file to the desktop from 10th line to 20.

6. Similarly, if you want to print from 10 to the end of line you can use the following command.

sed -n '10,$p' filename.txt

This is especially useful if we are dealing with a large file. Sometimes you just want to extract a sample without opening the entire file.

7. Command to print a specific line from a file. Here we can print 4th line.

sed -n '4p' filename.txt

8. SED command by default does not edit the original or source file for our safety but by using ‘-i’ option source file can be edited.

sed -i '1d' filename.txt

The above command will delete the First line from the file.

Few SED examples with different options

9. We can run multiple SED commands by using -e option, as shown in the below example

sed -e 's/unix/linux/' -e 's/testing/testingSED/' sample.txt

10. We can replace a string in multiple range of lines of a file. From the below example,the string will replace in 2nd and 3rd lines.

sed '2,3 s/unix/linux/' sample.txt

11. Using SED command, we can print each line of a file two times by adding 'p' print flag.

sed 'p' sample.txt

12. We can take the backup of files using SED command. Using the following command we take the backup of 'sample' file.

sed -i.bk 's/(name)/sample' sample.txt

There is much more to learn about SED command in UNIX. I hope you can spend some time to look them up. Hope fully this introduction with few basic commands will create interest and help you learn more about the command line stream editing. We will be updating new examples on SED command regularly. I recommend you to browse this post regularly.

  • by Syam
  • |
  • February 10, 2017
Click Here to Leave a Comment Below

Leave a Comment:

Get a Free Hosting Trial and Test Our Services. It Costs Nothing! Get it Here!  

x