In this tutorial, we will learn how to use the SED command, with the help of some SED command examples.
Sed command or Stream Editor is one of the most utilized utilities for Linux systems. It is mainly used for text substitution, find/replace operations & also other text manipulations like search, insertions deletion. We can also use regex or regular expressions with sed command, which further increases its usability. With the sed command, we can perform all the operations on a file without opening it.
In this article, we will learn to use the SED command by discussing some examples of the Sed command.
The syntax for using the sed command is,
sed options... [script] [InputFile]
Recommended Read: Learn to use Wget command with 12 examples
Also Read: Beginner’s reference guide to NMAP command
Sed commands examples
Print partial text of a file
To print only some part of a file onto the screen rather than seeing the whole file, use the following command,
$ sed -n 5,12p test.txt
Option ‘n’ used here will avoid printing of the whole file, while option ‘p’ will print only the line mentioned i.e. line 5 to 12.
Print all except some lines
To perform the reverse of the first example, i.e. to print complete contents of file except for some lines, use the following command,
$ sed 5,12d test.txt
Option ‘d’, here will remove the mentioned lines 5 to 12, from printing on to the screen.
Deleting a line
To remove a line from a file, use the following command,
$ sed 5d test.txt
where ‘5’ is the line number, which can be that you want & option ‘d’ will delete the mentioned line number.
Deleting a number of lines
To remove a number of lines from the file, we can use the following command,
$ sed ‘5-12d’ test.txt
This will delete lines 5 to 12 from the test.txt file.
Deleting lines other than the mentioned
To perform the reverse of above-mentioned command i.e. to delete lines other than the lines that are mentioned, we can use the following command,
$ sed ‘5-12!d’ test.txt
here `!`, will reverse the command mentioned i.e. will not delete the lines mentioned & all other lines will be deleted from the file.
Display every 5th line starting with a mentioned line
Now let’s say that we want to print every 5th line of a file starting with line number 3. To do that, we can use the following command
$ sed -n ‘3-5p’ test.txt
Replacing a word/string
To search a word or a string & then replace it with another string, we will use the following example,
$ sed ‘s/dan/susan/’ test.txt
Option ‘s’ here will search for the word ‘dan’ & then replace it with ‘susan’ for the first match it gets. Now if we need to replace the word completely from the file, we will use option ‘g’ along with ‘s’,
$ sed ‘s/dan/susan/g’ test.txt
Replace a particular occurrence of string/word
To replace a particular string of a string, for example, 8th occurrence from a file, use the following command,
$ sed ‘s/dan/susan/8’ test.txt
here, 8th occurrence of the word will be replaced from the file. Now to replace a string starting from a particular occurrence, use
$ sed ‘s/dan/susan/8g’ test.txt
So here, the 8th occurrence of the string will be replaced and all the matches after the 8th string.
Replace a string on a particular line
To change a word from a particular line only, use the following examples of sed command as a reference,
$ sed ‘9 s/dan/susan/’ test.txt
This will only substitute the string from the 9th line of the file. We can also mention a range of lines instead of a single line,
$ sed ‘9-13 s/dan/susan/’ test.txt
Change a whole line with matched pattern
We can also change a complete line after a search pattern has been matched. To do this use the following examples of sed command,
$ sed ‘/dan c “Changes for the new line here” ’ test.txt
So when the pattern matches ‘dan’, the whole line will be changed to the new line i.e. “Changes for the new line here”.
Add a line after/before the matched search
Now if we want to add a new line with some content & that too before a pattern match, then we can use the option ‘i’ with sed command & pattern to be searched,
$ sed ‘/dan i “Adding text before the match” ’ test.txt
To add a new line after every pattern match, the option that will be used with sed is an option ‘a’,
$ sed ‘/dan an “adding text after the match” ’ test.txt
Adding Blank lines/spaces
If we need to add a blank line after every non-blank line, we have to use option ‘G’,
$ sed G test.txt
Executing multiple sed commands
Now, if need to execute multiple sed expressions, we can do so using option ‘e’ to chain multiple sed commands. An example,
$ sed -e ‘s/dan/susan/g’ -e ‘s/hate/love/’ test.txt
Here we are searching for replacing ‘dan’ with ‘Susan’ & then also replacing ‘hate’ with ‘love’ from the first occurrence.
Making a backup copy before editing a file
Sed command has the power to alter complete files & which can be an issue if something goes wrong, so it’s better to create a backup copy before editing with sed. To create a backup copy of a file before we edit it, use option ‘-i.bak’,
$ sed -i.bak -e ‘s/dan/susan/g’ test.txt
So the backup file will be named test.txt.bak.
Delete a file line starting with & ending with a pattern with regex
As mentioned above, we can also use regex with the sed command to further enhance its capabilities. To delete a file line starting with a particular string & ending with another string, we can use the following examples of sed command,
$ sed -e ‘s/dan.*stops//g’ test.txt
This will delete the line with ‘dan’ on start & ‘stops’ in the end & it can have any number of words in between, ‘.*’ defines that part.
Appending lines with regex
Another example of using sed with regex would be to add some content before every line. So use the following example,
$ sed -e ‘s/.*/added text &/’ test.txt
Every line will now start with ‘added text’.
Deleting all commented lines & empty lines
To only remove commented lines, use the following example,
$ sed -e 's/#.*//' test.txt
To remove all commented lines i.e. lines with # & all the empty lines, use following example
$ sed -e 's/#.*//;/^$/d' test.txt
Use sed to search and replace spaces on the command line
In order to use sed to search and replace spaces on the command line, use the following command,
$ sed -e "s/ /,/g" < test.txt
Here we are looking for spaces & replacing them with comma.
Use sed to remove unwanted lines of text
You can use sed to remove unwanted lines of text from a file. For examples,
Delete lines which are in upper case or capital letters
$ sed '/^[A-Z]*$/d' test.txt
Delete lines that contain a pattern
$ sed '/pattern-here/d' test.txt
Delete lines starting from a pattern till the last line
$ sed '/pattern-here/,$d' test.txt
Delete the last line only if it contains the pattern
$ sed '${/pattern-here/d;}' test.txt
Get Help for the SED command
You can also get help for the sed command as you can for any other command. Just open the terminal & execute the following command,
$ sed --help
This was a brief tutorial on how to use the SED command with some SED examples. Please feel free to send in any queries or questions or suggestions you have using the comment box below.
What is /g here in commands
‘/g’ here means globally & used to replace the search term from whole file.
Thanks for sharing