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

Copy
$ sed {OPTIONS} filename

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

Copy
# cat linuxgeek.txt

Output

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‘.

Copy
# 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.

Output

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.

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

Output

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.

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

Output

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

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

Output

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

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

Output

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

Copy
# 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.

Output

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.

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

Output

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.

Examples

Syntax:

Copy
$ sed 'nd' filename.txt

To delete the 3rd line in the file execute

Copy
$ sed '3d' linuxgeek.txt

Output

Delete 3rd Line using sed command

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

Copy
$ sed '3,5d' linuxgeek.txt

Output

Delete A Range Of Lines Using Sed