‘Sed’ Command In Linux: Useful Applications Explained
Have you ever needed to replace some text in a file really quickly? Then you have to open up your text editor, find the line, and then type out your replacement. What if you have to do that many times? What if it isn’t also exactly the same thing and you have to run multiple searches and replace each occurrence? It gets tedious very quickly, but there’s a better way of doing it with a tool called sed.
We’ve written about POSIX and went over some of the interfaces and utilities a system must provide in order to be POSIX compliant. The command line tool sed is one of those utilities that provide a feature-rich way to filter, find, substitute, and rearrange data in texts files. It is an extremely powerful tool that is very easy to get started with, but very difficult to learn through and through due to its seemingly endless number of features.
First, we should note that the GNU implementation of sed, while POSIX compliant, goes above and beyond the specification to provide extensions and features not outlined in the POSIX requirement. When invoking sed you will want to use the –posix flag in order to disable all GNU-specific extensions and allow for more portability. This is key when writing scripts that you intend on running on various platforms. For example, the set of tools provided with macOS will not be the GNU tools, neither would those typically found on the *BSDs.
How to use sed command in Linux?
The general syntax is as follows
It’s a little cryptic, but it essentially expects the options followed by a set of instructions in textual form, and then the file to read from. In the case that you pipe text to sed, you can omit the input file.
There are several different modes that sed can operate in, some can be combined. The ones we’ll cover are the basics, but they’re still very powerful and become even more so as you develop your knowledge of sed and regular expressions.
- p – Print – prints lines with matching text
- d – Delete – deletes lines with matching text
- s – Substitute – substitutes, or replaces matching text on each line
In addition to the modes of operation, there are many flags that can be used at the end of a pattern text to augment the behavior of your command. We will be using the g flag to indicate global substitution instead of only replacing the first occurrence on each line, as per default.
Also Read: ‘Cut’ Command In Linux: Useful Applications Explained
Some sed command flags (as opposed to the pattern flags) we will use are as follows –
- n – suppresses automatic printing of text that doesn’t match
- E/r – enables extended regular expressions
- i – edits file in place rather than printing to screen
First, we need to discuss the notion of an address in sed. An address is simply a location in the text. When a text matches a provided pattern, the location of the match becomes the current address within which the command is executed. There is also support for address ranges in sed which allows for specifying a portion of a file for processing while leaving the rest alone.
Here, we can specify a range of lines to print –
We have specified lines 2 through 10 to be printed. The syntax in sed is odd, but it’s effective. We use the -n flag here to make sure that only that match are printed, otherwise all lines will be printed and those that match are duplicated.
NOTE: In sed, lines start at 1, not at 0.
Conversely, we can delete the same lines.
Since we’re not displaying only those that match, we want to remove our -n flag so that our lines print.
These examples are very contrived, so it’s hard to see how this can be very useful, but we’ll get there.
Next, we can start substituting occurrences of a particular pattern in a line. This is particularly useful for sysadmins. Whenever I install Linux on a new machine, I want to enable sshd, but I want to make sure that the root user cannot log in.
This command will search through /etc/ssh/sshd_config for the line containing #PermitRootLogin yes (with or without the #) and change it to PermitRootLogin no in place. The brackets allow for optional matching, which is very powerful. Different distributions ship with different defaults in configurations, so this is very handy.
NOTE: Be very careful when using the -i flag as you can easily lose work or destroy a system’s configuration.
Here we can compound some different functions in sed.
This is somewhat more advanced. What we’re doing here is telling sed to run the command on lines 2 through 10. The same can be done for printing or deleting all lines with a particular pattern match as well.
There are many functionalities in sed and there’s no way we can cover them all. If you like this tutorial, let us know and we’ll definitely do some more. Show us some of your favorite sed commands in the comments and feel free to request more command tutorials.
Also Read: The Ultimate A To Z List of Linux Commands | Linux Command Line Reference