Linux Sed Command Usage With Examples

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

Output

Linux sed Command Usage with Examples

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.

Output

Linux sed Command Usage with Examples

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

Output

Linux sed Command Usage with Examples

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

Output

Linux sed Command Usage with Examples

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

Output

Linux sed Command Usage with Examples

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

Output

Linux sed Command Usage with Examples

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.

Output

Linux sed Command Usage with Examples

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'

Output

Linux sed Command Usage with Examples

7. Delete lines from a file

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

Examples

Syntax:

$ sed 'nd' filename.txt

To delete the 3rd line in the file execute

$ sed '3d' linuxgeek.txt

Output

Linux sed Command Usage with Examples

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

$ sed '3,5d' linuxgeek.txt

Output

Linux sed Command Usage with Examples

  • 119