Linux sed Command

The sed command in Linux is a powerful stream editor used to perform basic text transformations on an input stream (a file or input from a pipeline). Below is a detailed guide with examples and descriptions of all options.

Basic Syntax

sed [options] 'script' inputfile

Common Options

Option Description
-n Suppress automatic printing of pattern space.
-e Allows multiple editing commands to be executed.
-f Reads sed commands from a file.
-i Edits files in place (modifies the original file).
-r Uses extended regular expressions.
--help Displays help information.

Examples

1. Replace a word in a file

sed 's/oldword/newword/' file.txt

This replaces the first occurrence of oldword with newword in each line of file.txt.

2. Replace all occurrences of a word

sed 's/oldword/newword/g' file.txt

The g flag replaces all occurrences of oldword with newword.

3. Delete lines containing a specific word

sed '/word/d' file.txt

This deletes all lines containing word in file.txt.

4. Print specific lines

sed -n '2,4p' file.txt

The -n option suppresses automatic printing, and 2,4p prints lines 2 to 4.

5. Edit a file in place

sed -i 's/oldword/newword/g' file.txt

This replaces all occurrences of oldword with newword directly in file.txt.

6. Delete a line

sed '3d' file.txt

This deletes the third line of file.txt.

7. Append text after a specific line

sed '2a\This is appended text' file.txt

This appends "This is appended text" after the second line of file.txt.

8. Insert text before a specific line

sed '2i\This is inserted text' file.txt

This inserts "This is inserted text" before the second line of file.txt.

9. Change a line

sed '3c\This is the new line' file.txt

This changes the third line of file.txt to "This is the new line".

10. Use a script file

sed -f script.sed file.txt

This applies the sed commands written in script.sed to file.txt.

11. Use extended regular expressions

sed -r 's/(pattern1|pattern2)/replacement/' file.txt

This uses extended regular expressions to replace either pattern1 or pattern2 with replacement.

12. Print lines matching a pattern

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

This prints only the lines that match pattern in file.txt.

13. Replace a word in multiple files

sed -i 's/oldword/newword/g' file1.txt file2.txt

This replaces all occurrences of oldword with newword in both file1.txt and file2.txt.

14. Use sed with a pipeline

echo "Hello oldword" | sed 's/oldword/newword/'

This replaces oldword with newword in the output of the echo command.

15. Replace a word in a file with a backup

sed -i.bak 's/oldword/newword/g' file.txt

This replaces all occurrences of oldword with newword in file.txt and creates a backup of the original file as file.txt.bak.

16. Replace a word in a file with a specific delimiter

sed 's/oldword/newword/g' file.txt

This replaces all occurrences of oldword with newword using the pipe character | as a delimiter instead of the default slash /.

17. Replace a word in a file with a case-insensitive search

sed 's/oldword/newword/Ig' file.txt

This replaces all occurrences of oldword with newword in a case-insensitive manner.

18. Replace a word in a file with a specific line number

sed '3s/oldword/newword/' file.txt

This replaces the first occurrence of oldword with newword only on the third line of file.txt.

19. Replace a word in a file with a specific range of lines

sed '2,4s/oldword/newword/g' file.txt

This replaces all occurrences of oldword with newword only in lines 2 to 4 of file.txt.

19. Replace a mulitiple word in a file in a single command

sed -e 's/oldword1/newword1/g' -e 's/oldword2/newword2/g' file.txt

This replaces all occurrences of oldword1 with newword1 and oldword2 with newword2 in file.txt.

20. Replace a word in a file with a specific pattern

sed 's/oldword\([0-9]\+\)/newword\1/g' file.txt

This replaces all occurrences of oldword followed by one or more digits with newword followed by the same digits in file.txt.