Tuesday, April 29, 2025

20 Useful Sed Commands With Examples

 

Introduction

The sed (stream editor) command in Linux is a powerful tool for processing and transforming text. It is widely used in shell scripting and text processing tasks, allowing users to perform complex text manipulations with simple commands. In this post, we will explore 20 useful sed commands with examples, helping you harness the full potential of this versatile tool.

Understanding Sed

Sed is a non-interactive command-line tool that processes and transforms text in a data stream or file. It reads the input line by line, applies specified operations, and outputs the transformed text. Common use cases include text substitution, deletion, insertion, and more.

20 Useful Sed Commands With Examples

1. Basic Text Substitution

The basic text substitution command replaces occurrences of a pattern with a specified replacement.

sed 's/pattern/replacement/' file.txt

Example & Output:

echo "Hello World" | sed 's/World/Sed/'
Hello Sed

2. Global Substitution

The g flag performs a global substitution, replacing all occurrences of the pattern.

sed 's/pattern/replacement/g' file.txt

Example & Output:

echo "Hello World, World" | sed 's/World/Sed/g'
Hello Sed, Sed

3. Case-Insensitive Substitution

The I flag makes the substitution case-insensitive.

sed 's/pattern/replacement/I' file.txt

Example & Output:

echo "Hello WORLD" | sed 's/world/Sed/I'
Hello Sed

4. Substitution with Line Numbers

Specify line numbers to target specific lines for substitution.

sed '3s/pattern/replacement/' file.txt
$ cat file.txt
Linux
Unix
old
infotechys
bash
terminal
scripts
Hello World
...

Example & Output:

sed '3s/old/new/' file.txt
Linux
Unix
new
infotechys
bash
terminal
scripts
Hello World
...

This replaces the first occurrence of “old” with “new” on the third line of file.txt.

5. Delete Lines Matching a Pattern

Delete lines that match a specified pattern.

sed '/pattern/d' file.txt

Example:

sed '/delete/d' file.txt

This deletes all lines containing the word “delete” in file.txt.

6. Delete a Specific Line

Specify a line number to delete that particular line (Replace ‘X’ with line number).

sed 'Xd' file.txt
Linux
Unix
new
infotechys
bash
terminal
scripts
Hello World
...

Example & Output:

sed '5d' file.txt
Linux
Unix
old
infotechys
terminal
scripts
Hello World

This deletes the fifth line in file.txt.

7. Insert Text Before a Line

Insert text before a specific line.

sed '3i\text' file.txt

Example & Output:

sed '3i\This is a new line' file.txt
Linux
Unix
This is a new line
old
infotechys
bash
terminal
scripts
Hello World

This inserts “This is a new line” before the third line in file.txt.

8. Append Text After a Line

Append text after a specific line.

sed '3a\text' file.txt

Example & Output:

sed '3a\This is an appended line' file.txt
Linux
Unix
old
This is an appended line
infotechys
bash
terminal
scripts
Hello World

9. Replace Only the First Occurrence

Replace only the first occurrence of a pattern.

sed '0,/pattern/s/pattern/replacement/' file.txt

Example & Output:

echo "apple apple apple" | sed '0,/apple/s/apple/orange/'
orange apple apple

10. Replace Only the Last Occurrence

Replace only the last occurrence of a pattern.

sed '$s/pattern/replacement/' file.txt

Example & Output:

echo "apple apple apple" | sed '$s/apple/orange/'
apple apple orange

11. Replace on a Range of Lines

Specify a range of lines for substitution.

sed '2,4s/pattern/replacement/' file.txt

Example:

sed '2,4s/old/new/' file.txt

This replaces “old” with “new” on lines 2 through 4 in file.txt.

12. Print Only Matching Lines

Print only lines that match a pattern.

sed -n '/pattern/p' file.txt

Example:

sed -n '/match/p' file.txt

This prints only lines containing the word “match” from file.txt.

13. Print Line Numbers of Matching Lines

Print line numbers of lines that match a pattern.

sed -n '/pattern/=' file.txt

Example:

sed -n '/match/=' file.txt

This prints the line numbers of lines containing the word “match” from file.txt.

14. Replace Text with Line Number

Replace a pattern with the line number.

sed = file.txt | sed 'N;s/\n/ /'

Example & Output:

sed = file.txt | sed 'N;s/\n/ /'
1 Linux
2 Unix
3 old
4 infotechys
5 bash
6 terminal
7 scripts
8 Hello World

This inserts the line number at the beginning of each line in file.txt.

15. Perform Multiple Substitutions

Perform multiple substitutions with a single command.

sed -e 's/old1/new1/' -e 's/old2/new2/' file.txt

Example:

sed -e 's/apple/orange/' -e 's/banana/grape/' file.txt

This replaces “apple” with “orange” and “banana” with “grape” in file.txt.

16. Use Regular Expressions

Use regular expressions for advanced pattern matching.

sed 's/[0-9]\+/number/g' file.txt

Example & Output:

echo "123 abc 456" | sed 's/[0-9]\+/number/g'
number abc number

17. Replace Text in Place

Edit the file in place, saving the changes directly to the file.

sed -i 's/old/new/g' file.txt

Example:

sed -i 's/old/new/g' file.txt

This replaces all occurrences of “old” with “new” and saves the changes to file.txt.

18. Add a Suffix to In-Place Edits

Create a backup when editing a file in place by adding a suffix to the original file.

sed -i.bak 's/old/new/g' file.txt

Example:

sed -i.bak 's/old/new/g' file.txt

This replaces all occurrences of “old” with “new” and saves the original file as file.txt.bak.

19. Use Sed in Shell Scripts

Incorporate sed commands in shell scripts for automation.

sed -i.bak 's/old/new/g' file.txt

Example script:

#!/bin/bash
# Script to replace 'old' with 'new' in all .txt files

for file in *.txt; do
  sed -i 's/old/new/g' "$file"
done

This script replaces “old” with “new” in all .txt files in the current directory.

20. Delete Blank Lines

Remove all blank lines from a file.

sed '/^$/d' file.txt

Example:

sed '/^$/d' file.txt

This deletes all blank lines in file.txt.

No comments:

Post a Comment

HTTP Appache Server LAB 7

 Apache HTTP Server (httpd) Configuration,