Linux
Sed command in Linux with usage examples [2023]
Let’s Learn Sed command in Linux with Practical Examples :
Short for stream editor, the sed command in Linux is a useful utility used for manipulation text output. You can use it for substituting text, finding and replacing text, searching text and so much more. The command accord you the ability to edit files without having to open them. In this tutorial, we look at various ways you can use the Sed command in Linux.
1) Replacing or substituting strings
To elaborate on how the command can be used for text manipulation, we are going to use a sample text file linuxgeek.txt
with the content below:
1 |
<br> Unix is a superb and powerful os. Unix is opensource. Unix is free os.<br> Unix has very many applications.<br> Linux was derived from Unix.<br> Unix is very easy to learn. Unix is a multiuser os. Learn Unix . |
To replace all instances of the word Unix with Linux, run the command:
1 |
$ sed 's/Unix/Linux/' linuxgeek.txt |
Output
The ‘s’ flag stands for substitution. In the command above, the first instance of the word ‘Unix’ is replaced by ‘Linux’. By default, the substitution happens to the first string occurrence only, and not the subsequent occurrences.
2) Replace the nth occurrence of a word in a line
To replace the nth occurrence of a string in a file, you need to use the /nth flag. For example, to replace the 2nd occurrence of a string in a line, issue the command:
1 |
$ sed 's/Unix/Linux/2' linuxgeek.txt |
Output
In the example above, the second instance of the string ‘Unix’ has been replaced by ‘Linux’ in the 1st and 4th lines.
3. Substitute all instances of a string in a file
If you want to replace all instances of the string to be substituted, in this case, ‘Unix’ use the /g option. The ‘g’ stands for global and instructs sed to replace all the instances of the string.
1 |
$ sed 's/Unix/Linux/g' linuxgeek.txt |
Output
4) Replace a string on a specific line number
Additionally, you can instruct the sed command in Linux to replace the keyword in a particular line. For example to replace the string in the 4th line only, use the command:
1 |
$ sed '4 s/Unix/Linux/' linuxgeek.txt |
Output
As observed, only the first instance is substituted. To substitute all the occurrences of the string, simple append ‘g’ after the last forward slash as shown:
1 |
$ sed '4 s/Unix/Linux/g' linuxgeek.txt |
Output
5) Substitute a string in a range of lines
You can also opt to replace the search keyword for a range of lines, for example between lines 1 and 3. To accomplish this, issue the command:
1 |
$ sed '1,3 s/Unix/Linux/g' linuxgeek.txt |
Output
From the output above, we can observe that the substitution has only occurred in lines 1-3. The last line remains unchanged.
6) Print lines where substitution has occurred
To print the lines where substitution of the lines has occurred, use the -n flag immediately after the sed command as shown:
1 |
$ sed -n '1,3 s/Unix/Linux/p' linuxgeek.txt |
Output
7) Delete lines using Sed command in Linux
Sed command in Linux can also be used for deleting lines. This is possible using the ‘d’ flag. To delete the 3rd line, for example, run the command:
1 |
$ sed '3d' linuxgeek.txt |
Output
To delete the last line only in the file, we are going to run the command:
8) Delete the last line
1 |
$ sed '$d' linuxgeek.txt |
Output
9) Delete a range of lines
To delete a range of lines. for instance from 2 to 4 in a text file, run the command:
1 |
$ sed '2,4d' linuxgeek.txt |
Output
10) Add blank lines or spaces
To add a blank line after every line run the command:
1 |
$ sed G linuxgeek.txt |
This wraps up our tutorial today. We have showcased some of the commonly used cases of the stream editor sed command.
-
DevOps55 years ago
Saltstack Tutorial for beginners [2023]
-
DevOps55 years ago
How to build a Docker cron job Container easily [2023]
-
Linux55 years ago
mail Command in Linux/Unix with 10+ Examples [2023]
-
DevOps55 years ago
Docker ADD vs COPY vs VOLUME – [2023]
-
DevOps55 years ago
How to setup Pritunl VPN on AWS to Access Servers
-
Linux55 years ago
Grep Command In Unix/Linux with 25+ Examples [2023]
-
Linux55 years ago
How To setup Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04
-
Linux55 years ago
Find command in Unix/Linux with 30+ Examples [2023]
1 Comment