Linux sed command with examples

In this article, we will see Linux sed command with examples.

sed command in Linux is stands for stream editor. It is used to perform lot of operation on a file like finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. We can edit files with opening them. Linux/Unix sed command supports regular expressions which allows it perform complex pattern matching.

Linux sed command syntax:

Note:You can find option at the end of the post.

sed [OPTION]... {script-only-if-no-other-script} [input-file]

Linux sed command examples

We use following content for sed command examples:

Linux sed command with examples

1. View ranges of line in Linux using command.

sed -n '2,5p' r2schools.log

It lines from 2nd line to 5th lines.
p means print.

2. Display last line of file in Linux:

sed -n '$p' r2schools.log

3. Display specific line twice times of file in Linux.

sed '3p' r2schools.log

4. View all lines except a given range of lines in Linux:

sed -n '2,4!p' r2schools.log

The above command prints lines except 2 to 4 lines.

5. View all lines wit multiple given range of lines in Linux:

Following examples extracts lines from 1 to 3 and 5 to 7 lines of given file. For multiple lines, we have to use e(means expression).

sed -n -e '1,3p' -e '5,7p' r2schools.log

6. View all lines of a file having specific word in Linux:

sed -n '/database/p' r2schools.log

The above command prints all lines which are having word ‘database’ in the file r2schools.log.

7. Replace or substitute a word in Linux:

sed 's/database/database Server/' r2schools.log

Where s means substitution.

8. Replace all occurrences of a text for particular line of file in Linux:

This can be achieved by using ‘g’ option.

sed 's/database/database Server/g' r2schools.log

Linux sed command is case-sensitive. To ignore case, we have to use i option like below. If don’t provide i, then Database and database both are different.

sed 's/database/database Server/gi' r2schools.log

The replacements are not permanent, these are permanent for displaying content on the terminal. If we want to make the changes permanent, we have to use -i option after sed command.

9. To replace text permanently in Linux file. Following command won’t display anything to the console.

sed -i 's/database/database server/gi' r2schools.log

10. Replace the text only in particular line Linux using sed command:

Following command replaces the database server to database only in 4th line permanently(-i) and for all occurrences(g).

sed -i '4 s/database server/database/gi' r2schools.log

11. To delete every occurrence of a file in Linux:

If we want to delete server from the lines 1,2 and 5. Then, run following commands.

sed -e 's/\//gi' r2schools.log

To delete permanently a word of a file in Linux, we have to provide -i option

sed -i -e 's/\//gi' r2schools.log

12. Delete particular line of a file in Linux using sed command.

sed '5d' r2schools.log

Linux sed command with examples

To delete permanently a word of a file in Linux, we have to provide -i option

sed -i '5d' r2schools.log

13. To delete last line of a file in Linux:

sed '$d' r2schools.log

To delete last line of a file permanently in Linux using sed command:

sed -i '$d' r2schools.log

14. To delete range of lines in file using sed command:

sed '3,5d' r2schools.log

To delete permanently a word of a file in Linux, we have to provide -i option

15. To delete blanks lines of a file in Linux:

sed -i '/^$/d' r2schools.log

16. To delete the content of a file.

sed d r2schools.log

17. If we want to delete the content of a file permanently using sed:

sed -i d r2schools.log

18. If we want get only Linux login names:

sed 's/:.*//' /etc/passwd

19. Insert lines beneath matched word line.

Where a means append below to the matched line.

sed '/database/a --Learn above technology for DBA Position' r2schools.log

If we want above change permanent, then use -i option

20. If we want insert line above to matching word line, then use i option instead of a after the word as show below.

sed '/database/a --Learn below technology for DBA Position' r2schools.log

21. Insert line after specified line in Linux.

sed '3 i Open source database' r2schools.log

22. Insert blank line after particular line of a file in Linux:

sed '3G' r2schools.log

23. To insert blank line after each line of a file in Linux.

24. Insert a black line above every line which matches a word.

sed '/MongoDB/{x;p;x;}' r2schools.log

25. Add numbers to each line of a file in Linux using sed command. It is like cat -n filename.

sed = r2schools.log | sed 'N;s/\n/\t/'

26. Save changes permanently or redirect to new file.

So far, all of our results have printed to the terminal window, but we haven’t yet saved them anywhere. To make these permanent, you can either write your changes to the original file or redirect them to a new one.

As discussed, if we want to save permanently we have to use -i option. But, If we want to save changes another file by adding extension with same filename run the following command. It is going to add blank line after 3rd line and saves the output to new file.

sed '3G' r2schools.log >>r2schools.sh

In this guide we have covered as much as possible for Linux sed command with examples.