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.txtExample & Output:
echo "Hello World" | sed 's/World/Sed/'Hello Sed2. Global Substitution
The g flag performs a global substitution, replacing all occurrences of the pattern.
sed 's/pattern/replacement/g' file.txtExample & Output:
echo "Hello World, World" | sed 's/World/Sed/g'Hello Sed, Sed3. Case-Insensitive Substitution
The I flag makes the substitution case-insensitive.
sed 's/pattern/replacement/I' file.txtExample & Output:
echo "Hello WORLD" | sed 's/world/Sed/I'Hello Sed4. Substitution with Line Numbers
Specify line numbers to target specific lines for substitution.
sed '3s/pattern/replacement/' file.txt$ cat file.txtLinux
Unix
old
infotechys
bash
terminal
scripts
Hello World
...
Example & Output:
sed '3s/old/new/' file.txtLinux
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.txtExample:
sed '/delete/d' file.txtThis 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.txtLinux
Unix
new
infotechys
bash
terminal
scripts
Hello World
...
Example & Output:
sed '5d' file.txtLinux
Unix
old
infotechys
terminal
scripts
Hello WorldThis deletes the fifth line in file.txt.
7. Insert Text Before a Line
Insert text before a specific line.
sed '3i\text' file.txtExample & Output:
sed '3i\This is a new line' file.txtLinux
Unix
This is a new line
old
infotechys
bash
terminal
scripts
Hello WorldThis 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.txtExample & Output:
sed '3a\This is an appended line' file.txtLinux
Unix
old
This is an appended line
infotechys
bash
terminal
scripts
Hello World9. Replace Only the First Occurrence
Replace only the first occurrence of a pattern.
sed '0,/pattern/s/pattern/replacement/' file.txtExample & Output:
echo "apple apple apple" | sed '0,/apple/s/apple/orange/'orange apple apple10. Replace Only the Last Occurrence
Replace only the last occurrence of a pattern.
sed '$s/pattern/replacement/' file.txtExample & Output:
echo "apple apple apple" | sed '$s/apple/orange/'apple apple orange11. Replace on a Range of Lines
Specify a range of lines for substitution.
sed '2,4s/pattern/replacement/' file.txtExample:
sed '2,4s/old/new/' file.txtThis 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.txtExample:
sed -n '/match/p' file.txtThis 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.txtExample:
sed -n '/match/=' file.txtThis 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 WorldThis 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.txtExample:
sed -e 's/apple/orange/' -e 's/banana/grape/' file.txtThis 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.txtExample & Output:
echo "123 abc 456" | sed 's/[0-9]\+/number/g'number abc number17. Replace Text in Place
Edit the file in place, saving the changes directly to the file.
sed -i 's/old/new/g' file.txtExample:
sed -i 's/old/new/g' file.txtThis 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.txtExample:
sed -i.bak 's/old/new/g' file.txtThis 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.txtExample 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"
doneThis 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.txtExample:
sed '/^$/d' file.txtThis deletes all blank lines in file.txt.
No comments:
Post a Comment