In this article, we will review commonly used tar and gzip commands in Linux with some examples. Linux users and IT professionals alike, ought to
Discover 20 useful sed commands for text processing in Linux. Learn how to perform substitutions, deletions, insertions, and more with practical examples and tips. Enhance your text manipulation skills with this comprehensive guide.
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.
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.
The basic text substitution command replaces occurrences of a pattern with a specified replacement.
sed 's/pattern/replacement/' file.txt
echo "Hello World" | sed 's/World/Sed/'
Hello Sed
The g
flag performs a global substitution, replacing all occurrences of the pattern.
sed 's/pattern/replacement/g' file.txt
echo "Hello World, World" | sed 's/World/Sed/g'
Hello Sed, Sed
The I
flag makes the substitution case-insensitive.
sed 's/pattern/replacement/I' file.txt
echo "Hello WORLD" | sed 's/world/Sed/I'
Hello Sed
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
...
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
.
Delete lines that match a specified pattern.
sed '/pattern/d' file.txt
sed '/delete/d' file.txt
This deletes all lines containing the word “delete” in file.txt
.
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
...
sed '5d' file.txt
Linux
Unix
old
infotechys
terminal
scripts
Hello World
This deletes the fifth line in file.txt
.
Insert text before a specific line.
sed '3i\text' file.txt
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
.
Append text after a specific line.
sed '3a\text' file.txt
sed '3a\This is an appended line' file.txt
Linux
Unix
old
This is an appended line
infotechys
bash
terminal
scripts
Hello World
Replace only the first occurrence of a pattern.
sed '0,/pattern/s/pattern/replacement/' file.txt
echo "apple apple apple" | sed '0,/apple/s/apple/orange/'
orange apple apple
Replace only the last occurrence of a pattern.
sed '$s/pattern/replacement/' file.txt
echo "apple apple apple" | sed '$s/apple/orange/'
apple apple orange
Specify a range of lines for substitution.
sed '2,4s/pattern/replacement/' file.txt
sed '2,4s/old/new/' file.txt
This replaces “old” with “new” on lines 2 through 4 in file.txt
.
Print only lines that match a pattern.
sed -n '/pattern/p' file.txt
sed -n '/match/p' file.txt
This prints only lines containing the word “match” from file.txt
.
Print line numbers of lines that match a pattern.
sed -n '/pattern/=' file.txt
sed -n '/match/=' file.txt
This prints the line numbers of lines containing the word “match” from file.txt
.
Replace a pattern with the line number.
sed = file.txt | sed 'N;s/\n/ /'
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
.
Perform multiple substitutions with a single command.
sed -e 's/old1/new1/' -e 's/old2/new2/' file.txt
sed -e 's/apple/orange/' -e 's/banana/grape/' file.txt
This replaces “apple” with “orange” and “banana” with “grape” in file.txt
.
Use regular expressions for advanced pattern matching.
sed 's/[0-9]\+/number/g' file.txt
echo "123 abc 456" | sed 's/[0-9]\+/number/g'
number abc number
Edit the file in place, saving the changes directly to the file.
sed -i 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt
This replaces all occurrences of “old” with “new” and saves the changes to file.txt
.
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
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
.
Incorporate sed
commands in shell scripts for automation.
sed -i.bak 's/old/new/g' file.txt
#!/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.
Remove all blank lines from a file.
sed '/^$/d' file.txt
sed '/^$/d' file.txt
This deletes all blank lines in file.txt
.
The sed
command is a powerful tool for text processing in Linux. With these 20 useful sed
commands, you can perform a wide range of text manipulations efficiently. Whether you are editing files, automating tasks, or processing data streams, mastering sed
will significantly enhance your productivity and capabilities in the Linux environment.
Did you find this article useful? Your feedback is invaluable to us! Please feel free to share your thoughts in the comments section below.
Related Posts
In this article, we will review commonly used tar and gzip commands in Linux with some examples. Linux users and IT professionals alike, ought to
Unlock the full potential of AWK with these popular commands that will streamline your text processing tasks and increase your productivity. Table of Contents Introduction
In this article, we will review the most commonly used RPM commands in Linux. These commands play a pivotal role in package management, allowing users