Linux sed Command Usage with Examples

Filed Under: UNIX/Linux

Sed, short for Stream EDitor, is a command that is used to perform text transformations and manipulations on a file. Some of these transformations include searching and replacing text. With Linux sed command, you can manipulate and edit text files without even opening them. In this tutorial, you will learn how to manipulate text files using sed command.

Linux sed command syntax

$ sed  {OPTIONS} filename

In this tutorial, we will be using linuxgeek.txt file as our reference file.

# cat linuxgeek.txt


linux sed test file

1. Substituting or replacing a string

In most cases, sed command is used for replacing strings of text.

The command below replaces ‘Linux‘ with ‘Unix‘.

# sed 's/Linux/Unix/' linuxgeek.txt
  • The s in the command is the replacement indicator.
  • The / are the delimiters
  • Linux is the search term
  • unix is the replacement term

By default, sed command replaces only the first occurrence in a line. The subsequent occurrences will not be substituted or replaced.


Linux sed command replace string

2. Substituting or replacing the nth occurrence in a line

If you want to replace the second string occurrence in a line, use the /2 flag as shown.

# sed 's/Linux/Unix/2' linuxgeek.txt


sed command Replace Nth Occurrence In A File

3. Substituing or replacing all occurrences of the pattern in a file

To replace all the occurrences of the search pattern in the file, use the /g flag. The /g is the global replacement.

# sed 's/Linux/Unix/g' linuxgeek.txt


Replace All Occurrences

4. Substituting or replacing all occurrences of a string in a specific line only

If you want to replace all occurrences of a string in a specific line, say in line 2, use the syntax below

# sed '2 s/Linux/Unix/g' linuxgeek.txt


Replace All Occurrences In A Specific Line

5. Substituting or replacing a string in a specific range of lines

If you want to specify the range of lines that the string replacement will occur, use the example shown below. The example instructs the replacement to occur from lines 1 -3

# sed '1,3 s/Linux/Unix/g' linuxgeek.txt


Replace a string in a  Range Of Lines

Additionally, you can replace text from a specific line to the end of the file as shown

# sed '2,$ s/Linux/Unix/g' linuxgeek.txt

What the command does is that it replaces all the occurrences from the 2nd line to the last line of the file.


Replace String From A Line To The End Of The File

6. Parenthesize the first character of every word

The example below demonstrates how you can put parenthesis on every first character of a word in a line.

$ echo "Hey Guys, Welcome To Linux Operating System" | sed 's/\(\b[A-Z]\)/\(\1\)/g'


Sed Command To Parenthesize First Character In A Word

7. Delete lines from a file

You can also use the linux sed command to delete lines in a file.



$ sed 'nd' filename.txt

To delete the 3rd line in the file execute

$ sed '3d' linuxgeek.txt


Delete 3rd Line using sed command

To delete a range of lines, say from line 3 to line 5 , run the command:

$ sed '3,5d' linuxgeek.txt


Delete A Range Of Lines Using Sed


  1. Sidney says:

    How can I use sed command to replace a field with an IP/mask value ?
    for example:
    field: 192.168.xx.xx
    IP/mask :

    This doesn’t work because the “/” is the delimiter. How can I update it? Thanks
    sed “s/192.168.xx.xx/”

  2. Vivek Pawar says:

    How to do below task

    Replace “man” with “woman” and “woman” with “man” at the same time in file

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages