What is SED!
sed stands for "stream editor," and it is a powerful text processing tool available in Unix-like operating systems, including Linux. It operates on text files or input streams and performs various editing operations based on specified commands.
sed stands for "stream editor," and it is a powerful text processing tool available in Unix-like operating systems, including Linux. It operates on text files or input streams and performs various editing operations based on specified commands.
Here are 20 practical examples of using the sed command in Linux 👇
1/20 - Remove leading/trailing whitespace:
$ echo " Hello, World! " | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
Output: "Hello, World!"
$ echo " Hello, World! " | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
Output: "Hello, World!"
2/20 - Replace a string in a file:
$ sed -i 's/foo/bar/g' file.txt
# Replaces all occurrences of "foo" with "bar" in file.txt
$ sed -i 's/foo/bar/g' file.txt
# Replaces all occurrences of "foo" with "bar" in file.txt
3/20 - Print specific lines from a file:
$ sed -n '5,10p' file.txt
# Prints lines 5 to 10 from file.txt
$ sed -n '5,10p' file.txt
# Prints lines 5 to 10 from file.txt
4/20 - Delete blank lines from a file:
$ sed '/^$/d' file.txt
# Deletes all blank lines from file.txt
$ sed '/^$/d' file.txt
# Deletes all blank lines from file.txt
5/20 - Find and replace using regular expressions:
$ sed 's/\(word\)\([0-9]\)/\2\1/g' file.txt
# Replaces "word1" with "1word" in file.txt
$ sed 's/\(word\)\([0-9]\)/\2\1/g' file.txt
# Replaces "word1" with "1word" in file.txt
6/20 - Append text to the end of each line:
$ sed 's/$/ - The End/' file.txt
# Appends " - The End" to the end of each line in file.txt
$ sed 's/$/ - The End/' file.txt
# Appends " - The End" to the end of each line in file.txt
7/20 - Delete lines matching a pattern:
$ sed '/pattern/d' file.txt
# Deletes all lines containing "pattern" from file.txt
$ sed '/pattern/d' file.txt
# Deletes all lines containing "pattern" from file.txt
8/20 - Print specific columns from a CSV file:
$ sed 's/,/\t/g' file.csv | cut -f 2,4
# Prints the 2nd and 4th columns of a CSV file, separated by tabs
$ sed 's/,/\t/g' file.csv | cut -f 2,4
# Prints the 2nd and 4th columns of a CSV file, separated by tabs
9/20 - Replace tabs with spaces:
$ sed 's/\t/ /g' file.txt
# Replaces tabs with four spaces in file.txt
$ sed 's/\t/ /g' file.txt
# Replaces tabs with four spaces in file.txt
10/20 - Insert a line before/after a pattern:
$ sed '/pattern/i This line will be inserted before the pattern' file.txt
# Inserts a line before the line containing "pattern" in file.txt
$ sed '/pattern/i This line will be inserted before the pattern' file.txt
# Inserts a line before the line containing "pattern" in file.txt
11/20 - Delete the last line of a file:
$ sed '$d' file.txt
# Deletes the last line from file.txt
$ sed '$d' file.txt
# Deletes the last line from file.txt
12/20 - Count the number of occurrences of a pattern:
$ sed -n 's/pattern/&/pg' file.txt | wc -l
# Counts the number of occurrences of "pattern" in file.txt
$ sed -n 's/pattern/&/pg' file.txt | wc -l
# Counts the number of occurrences of "pattern" in file.txt
13/20 - Perform case-insensitive search and replace:
$ sed 's/foo/bar/ig' file.txt
# Replaces all occurrences of "foo" with "bar" (case-insensitive) in file.txt
$ sed 's/foo/bar/ig' file.txt
# Replaces all occurrences of "foo" with "bar" (case-insensitive) in file.txt
14/20 - Reverse the order of lines in a file:
$ sed '1!G;h;$!d' file.txt
# Reverses the order of lines in file.txt
$ sed '1!G;h;$!d' file.txt
# Reverses the order of lines in file.txt
15/20 - Delete lines between two patterns:
$ sed '/start/,/end/d' file.txt
# Deletes all lines between "start" and "end" (inclusive) from file.txt
$ sed '/start/,/end/d' file.txt
# Deletes all lines between "start" and "end" (inclusive) from file.txt
16/20 - Print line numbers along with matching lines:
$ sed -n '/pattern/=' file.txt
# Prints line numbers of lines containing "pattern" in file.txt
$ sed -n '/pattern/=' file.txt
# Prints line numbers of lines containing "pattern" in file.txt
17/20 - Delete all lines except the first and last:
$ sed '1n; $!{H;d};x' file.txt
# Deletes all lines except the first and last from file.txt
$ sed '1n; $!{H;d};x' file.txt
# Deletes all lines except the first and last from file.txt
18/20 - Extract text between two patterns:
$ sed -n '/start/,/end/p' file.txt
# Prints the lines between "start" and "end" (inclusive) from file.txt
$ sed -n '/start/,/end/p' file.txt
# Prints the lines between "start" and "end" (inclusive) from file.txt
19/20 - Increment a number in a file:
$ sed 's/[0-9]\+/\n&\n/g;:a; s/\n0*\([0-9]\+\)\n\n/\n\1\n/;ta' file.txt
# Increments a number found in file.txt by 1
$ sed 's/[0-9]\+/\n&\n/g;:a; s/\n0*\([0-9]\+\)\n\n/\n\1\n/;ta' file.txt
# Increments a number found in file.txt by 1
20/20 - Limit the number of replacements per line:
$ echo "foo foo foo foo" | sed 's/foo/bar/2'
Output: "bar bar foo foo"
# Replaces the first two occurrences of "foo" with "bar"
$ echo "foo foo foo foo" | sed 's/foo/bar/2'
Output: "bar bar foo foo"
# Replaces the first two occurrences of "foo" with "bar"
That's it! I hope you find these examples helpful for mastering the sed command in Linux. Let me know if you have any questions!
Retweet the thread if you find it useful. Thanks!
Loading suggestions...