What is the SED Command in Unix?
SED is a command in Unix which is a powerful text editor used to insert, search, delete, replace etc. SED also called Stream Editor which performs pattern matching that are complex by supporting regular expression which is a string describing the character sequences. SED command is really quick which can find and replace an expression even when the file is not opened whose syntax includes action and pattern where action is the command to be executed for a given pattern and pattern is a sequence of characters or a regular expression on which the action needs to be performed.
- Action: is the command to be performed on the given pattern.
- Pattern: It is a regular expression. Basically, the stream of characters on which the given action is supposed to be performed. If the pattern is omitted, then the given action is performed on all the lines.
- /: It is used as a delimiter.
|P||Prints the Line|
|d||Deletes the Line|
|s/pattern1/pattern2/||Substitute the Occurrence of Pattern1 from Pattern2|
Some of the wildcard character used with the SED Command:
|*||Matches zero or more occurrence of the previous character|
|$||End of The Line|
|^||Beginning of The Line|
SED Command Usage
Below are the usage of SED Command in an Expression way:
Below are some of the substitution expression
1. Substituting String: Replaces the word Country with India in the file sample.txt
$sed ‘s/Country/India’ sample.txt
In the command above sed – command
- s: Substitution
- /: Delimiter
- Country: Search String
- India: Replacement String
By default, SED replaces the first occurrence of the word ‘Country’ in each line and performs no action on second, third occurrence.
2. Substitution of nth Occurrence: Replaces the second occurrence of the word Country with India in the file sample.txt. In the same way, we can replace the occurrence of the nth word in a line.
$sed ‘s/Country/India/2’ sample.txt
3. Substitution of All the Occurrence: Replaces all the occurrence of the word.
$sed ‘s/Country/India/g’ sample.txt
Here, /g means Global Replacement. All the occurrence of the word Country is replaced by India.
4. Substituting from nth Occurrence to All the Occurrence in a Line: It is a combination of the above two commands.
$sed ‘s/Country/India/3g’ sample.txt
The above command will replace the pattern ‘Country’ by ‘India’ from all the places starting from the third position.
$sed ‘s/Country/India/3gi’ sample.txt
- We can also use gi in place of g. It will simply ignore the case of the character
- We can also perform the replace operation at a specific line number.
5. Replacing String at Specific Line Number
$sed ‘3 s/Country/India/’ sample.txt
The command will replace Country with India only in the Third line.
6. Replacing String on A Range of Line
$sed ‘1,3 s/Country/India/’ sample.txt
The above command will replace the string ‘Country’ with ‘India’ from the lines 1 to 3
$sed ‘4,$ s/Country/India/’ sample.txt
7. $ – Indicates the Last Line: This will perform the replace operation from starting from 4th line to the end of the file.
Let us now see the use of the print expression.
1. Duplicating the Line
$sed ‘s/Country/India/p’ sample.txt
Once the replace operation has been performed. It will duplicate that specific line. It will not perform any duplicate operations on the line where there is no substitution performed primarily.
2. Printing only the Substituted Lines: Use of -n option along with print expression prints only the lines where substitution has taken place. In other words, we can say that it will skip the duplicate lines.
$sed -n ‘s/Country/India/p’ sample.txt
3. Printing Partial Text from A File: The below command will display the lines from 3rd till 7th of the given file name.
$sed -n ‘3,7p’ sample.txt
4. Viewing Non-Consecutive Line Ranges: Command will display lines from 3 to 7th position and lines from 10 to 16th position.
$sed -n -e ‘3,7p’ -e ’10,16p’ sample.txt
Option -e is used to execute the commands.
This command is used for deleting the lines from a file. We can even delete a line from a file without even opening the file.
1. Delete Any Specific Line: deletes the line of a given line number.
$sed ‘nd’ sample.txt
$sed ‘5d’ sample.txt
2. Delete the Last Line of A File: Presence of $d simply removes the last line of the file.
$sed ‘$d’ sample.txt
3. To Delete Line in A Given Range: The command will delete the line from a file in the given range. Here it will delete the lines starting from 3rd till 6th.
$sed ‘3,6d’ sample.txt
The below command will delete the lines starting from 3rd to the last line
$sed ‘3, $d’ sample.txt
4. Delete a Line Having a Given Pattern: Looks for that specific pattern and delete the line containing that pattern.
$sed ‘/pattern/d’ sample.txt
5. Inserting a Space: ‘G’ upper case G is used to add space after every non-empty line in a file.
$sed G sample.txt
Two blank lines will be added if we put ‘G; G’
$sed ‘G;G’ sample.txt
6. Removing the Blank Line in A File: With the use of the wild card character mentioned at the start of the article, we can remove the blank line of a file.
$sed ‘/^$/d’ sample.txt
- ^: Represents the start of the line
- $: Representing the End of The Line
- ^$: Represents the Empty Line
Combining it with d will remove the blank lines from the given file.
7. Editing the Source File: By default, the source file will not be edited by performing any operation. But the use of -I option will edit the source file as well.
$sed -I ‘3d’ sample.txt
This command will delete the 3rd line from the source file
8. Backup of a File: The command can be used to take the backup of the given file
$sed -i.bk ‘s/Newname/sample’ sample.txt
Familiarity with operations on a file to filter the content of a file, replace some pattern and delete some patterns can be very useful for the people dealing with text files. SED is a powerful command. It can also be combined with various other commands using ‘|’ to create a more powerful command.
This is a guide to SED Command in Unix. Here we discuss the basic concept, usages of SED Command in an Expression way with syntax. You can also go through our other related articles to learn more –