Sed Command in Linux
We offer you a brighter future with FREE online courses - Start Now!!
In this article, you will learn all there is to the SED command in Linux-based operating systems. We will be going through, what SED is, how it works, the syntax, and the options of the SED command. We will also be looking into some practical examples of the command in the terminal.
What is SED Command in Linux?
SED is an abbreviation for “Stream Editor”. It is a command-line-based utility in Linux-based operating systems that helps in editing streams. It may sound simple when you say ‘editing’, but what it really does is searching, finding, replacing, sorting, inserting, and deleting, now it sounds fun!
The sed command can either be used by piping it with some other main command or simply using it on a text file. When we talk about “editing” using sed, it is done using commands only. Sed does not have an interactive text editor interface like nano, cat, or vi, you have to do all the process of editing solely using commands.
Using sed we can select the text, delete the line from the text, substitute text (find and replace) modify, and delete text. The best part about sed is, you don’t even have to open files to edit them (unlike vi and nano), thus making the workflow easier and efficient. Before we go any further, let us look at 1 really simple example of sed to take in the unorthodox text editor because sed is not your average text editor, it’s far better!
Let us take the simple command: echo DataFair | sed -e ‘s/Fair/Flair/’
This command will give the following output:
As you saw, we found and replace the string pattern “Fair” with “Flair”.
How does the SED command work?
Sed contains 2 data buffers that are both initially empty, they are pattern space and auxiliary hold space. When sed is invoked, it read one line from the input text, (removes trailing lines if any), and stores it in the pattern space. Then the commands are executed, and each command has an address associated with it.
When sed reaches the end of the script, the contents of the pattern space are printed out to the output stream. And when it prints out the output, it also adds back the trailing lines which were removed earlier.
Syntax of sed command in linux
The syntax of the sed command may look slightly intimidating at first, but when we discuss what the fields re and why they are used, you will get the hang of it, The syntax of the sed is command is:
sed <options> <script> <input file>
Let us look at the fields one by one:
1. Options:
This field takes in options that specify how sed should function.
2. Script
This field specifies what editing should be done on the file.
3. Input file
This field takes in the file that has to be edited.
Options used with Linux SED command
As we saw, the options file in the syntax of the sed command takes in a range of options that specifies how the sed command should work and how the output should be formatted. Let us look at the options available with the SED command.
1. -n
This option suppresses the automatic printing of pattern space. You can also write this option as “–quite” or “–silent”.
2. -e <script>
This option adds the script to the command you want to execute. You can also write this command as “–expression <script>”
3. -f <script-file>
This option adds the contents of the script file to the commands to be executed. You can also write this command as “–file <script file>”
4. –follow-symlinks
This option follows symlinks when processing is happening.
5. -i <SUFFIX>
This option edits files in place. You can also write this command as “–in-place <SUFFIX>”
6. –help
This option displays the help menu as shown below
7. –POSIX
This option disables all the GNU extensions.
8. -r
This option uses extended regular expressions in th script. You can also write this command as “–regexp-extended”.
9. -s
This option considers files as separate rather than as a long single continuous stream. You can also write this command as “–separate”
10. -u
This option Load minimal amounts of data from the input files and flush the output buffers more often. You can also write this option as “–unbuffered”.
11. -I <N>
This option specifies the desired line warp length <N>, you can also write this command as “–line-length <N>”
12. –version
This option displays information about the version of sed you are using.
Now that we have laid down the fundamentals, let us put all this knowledge together and see some practical examples of the sed command in action.
Selecting a range of lines
If you want to select lines in a document between a specific range, use the option “-n” followed by the range you want to see as follows:
sed -n ‘<n1>,<n2>p’ <filename>
Let us consider the following text file:
If I run the command “sed -n ‘5,10p’ file.txt”, I will get only the lines from 5 to 10 as shown below:
Viewing the entire file except for the selected range
Let us now try to do the negative of the proviso output, let us print the entire file, except the selected range. To do so, use the same command as used above, without the option “-n” as follows:
sed ‘<n1>,<n2>d’ <filename>
For example, the command “sed ‘5,10d’ file.txt”, will print the entire file except for the line from 5-10.
Viewing nonconsecutive lines and ranges
You can also choose to display nonconsecutive lines of the file. For example, if you want to display the lines from 2-6 and 9-11, use the command as shown:
sed -n -e ‘2,6p’ -e ‘9,11p’ file.txt
Replacing words and characters
To replace every occurrence of a specific word or character, use the following syntax:
sed ‘s/<old string>/<new string>/g’ <filename>
For example, If I want to replace the word “Debian” with “Ubuntu”, I will use the command:
sed ‘s/<Debian>/<Ubuntu>/g’ file.txt
Notice that when I specified “Debian” I specified it exactly how it was (notice th case). However, if you want to ignore the case of the word, use “gi” instead of “g”.
Notice that when I used “Debian” with “g”, it did not replace it, however, when I used “gi”, it was successfully replaced.
Replacing words only within a specific range
If you want to replace words only in a specific range and not though out the file, you can use the syntax shown below:
sed ‘<n1>,<n2> s/<old string>/<new string>/g’ <filename>
For example, If I want to replace the name “GNU” with “GPL” only in the range 9-11, I use the command shown below:
sed ‘9,11 s/GNUGPL/g’ file.txt
In the above output, the text “GNU” in the 9th and 10th lines has been changed (marked in blue), but the word “GNU” in the 4th line remained unchanged (marked in yellow).
Substituting using regular expressions
We all know that all the configuration files have comments in them, what if we wanted to read only the comments and not any other content? Well we cal make use of regular expression and filter out the lines starting with a particular character.
We know that all the comments start with a hash (#) so we can replace the hash character with a dollar sign ($) to count them as blank lines.
Viewing lines that contain a specified pattern
If you want to print the lines that contain the pattern you specify, use the following syntax:
sed -n ‘/^<pattern>/ p’ <filename>
For example, if you want to display the line that starts with the word “Copyright”, you can use the following command:
sed -n ‘/^Copyright/ p’ file.txt
Inserting space in files
We can also insert blank lines between the content using the sed command. To insert a single blank line, use the option “G” and to insert 2 blank lines, use the option “G;G”.
In-place editing and backing up a file.
Using the sed command, you can also create a backup and edit the file in one single command.
In the above output, I created a back u file and replaced the word “Debian” with “RHEL”.
Switching a pair of words
Say that you have a file where you may want to exchange the position of 2 words, you can do so, by using the syntax shown below:
sed ‘s/^\(.*\),\(.*\)$/\, \g’ <filename>
For example, consider the following text file:
Upon running the command “sed ‘s/^\(.*\),\(.*\)$/\, \g’ expression.txt”, will give the following output:
Replacing words only if a separate match is found
We can take substitutions using the SED command to the next level, by fixing specific conditions, we can replace a word if and only if another specified word exists in the same sentence.
For example, if you want to replace the word “GNU” with License if and only if the word “GPL” exists in the sentence.
Substitution 2 or more words at once
You can take substitution even further, by replacing 2 or more words in just one command. This is not as anticipated as it sounds. You just write the same syntax of replacing strings one after the other separated by a semicolon (;).
For example, If I want to replace the text “Debian” with “RHEL” and “GNU” with “GPL”, in just one command, I used the following command:
sed -i ‘s/Debian/RHEL/g’;’s/GNU/GPL/gi’ file.txt
Combining sed with other commands
In the very first section itself, we say that sed could be combined with other commands. In fact, we can combine one sed command with another sed command. All this connecting can simply be done by using pipes (|). Here is an example:
Printing line numbers
To print the line numbers of a file, use ‘=’ as shown:
sed ‘=’ <filename>
If you want to know the line numbers of a specific word, you can use the command: sed -n ‘/<word>/=’ <filename>
Modifying lines
To modify a specific line, use the option “c” as shown below:
sed ‘<n>c\<sentence>’ <filename>
Inserting and appending text
We saw that the flags “i” and “a” are for inserting text, however, the key difference between the 2 is that “i” inserts before the text, and “a” inserts after the text.
Removing a line
To remove a line from a file, use the flag “d” as shown below:
Parenthesize the first character of each word
Use the command shown below to enclose the first cahectar pof each owrd in a parentheses:
echo <string> | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Deleting the last line
To delete the last line of the file make use of the option “d” as shown below:
sed ‘$d’ <filename>
Deleting lines in a range
To delete lines between a specific range, use the option “d”, prefixed by the range as shown:
sed '<n1>,<n2>d' <filename>
Deleting from the nth line to the last line
To delete the set of lines from a specific line all the way to the end, use the option “d” prefixed by the line number you want to delete from as shown:
$ sed '<n>,$d' <filename>
Using addresses
The sed command also support addresses, you can specify the address of a file rather that going into that directory and writing its name, here is an example:
Summary
As you have seen, the sed command is not your traditional text/stream editor, I would reckon it is far better! Even though the changes you make to the file are not permanent, it gives you infinite possibilities of editing streams and text, as we have seen with 25 beautiful examples.
You have now learned what sed is, why we use it, how it works, the syntax, options used with the command, and 25 wonderful examples of the sed command in action.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google