Updated May 31, 2023
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. The SED command is highly efficient and capable of finding and replacing expressions, even without opening the file. Its syntax consists of an action and a pattern. In the SED command, the action represents the command to be executed, and the pattern is a sequence of characters or a regular expression on which the action is performed.
Syntax:
/pattern/action
- Action: is the command to be performed on the given pattern.
- Pattern: It is a regular expression. You are expected to execute the given action on the stream of characters.Performing the given action on all the lines occurs when the pattern is omitted.
- /: It is used as a delimiter.
Expression | Description |
P | Prints the Line |
d | Deletes the Line |
s/pattern1/pattern2/ | Substitute the Occurrence of Pattern1 from Pattern2 |
Some of the wildcard characters used with the SED Command:
Character | Description |
* | Matches zero or more occurrences of the previous character |
$ | End of The Line |
^ | Beginning of The Line |
. | Single Character |
SED Command Usage
Below are the usage of SED Command in an Expression way:
Substitution Expression
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 the second or 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. India replaces all occurrences of the word “Country”.
4. Substituting from nth Occurrence to All the Occurrence in a Line: It combines the above two commands.
$sed 's/Country/India/3g' sample.txt
The above command will replace the pattern ‘Country’ with ‘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 lines 1 to 3
$sed '4,$ s/Country/India/' sample.txt
7. $ – Indicates the Last Line: This will perform the replace operation from the 4th line to the end of the file.
Print Expression
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. Primarily, the line where no substitution is performed will not undergo any duplicate operations.
2. Printing only the Substituted Lines: Use of the -n option and print expression prints only the lines where substitution occurs. 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 to the 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 the 7th and from 10 to the 16th.
$sed -n -e '3,7p' -e '10,16p' sample.txt
Option -e is used to execute the commands.
Delete Expression
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
Example:
$sed '5d' sample.txt
2. Delete the Last Line of A File: Presence of $d 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 the 3rd till the 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: Using 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 using the -I option will also edit the source file.
$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
Conclusion
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 people dealing with text files. SED is a powerful command. You can combine the stream of characters with various other commands using ‘|’ to create a more powerful command.
Recommended Articles
We hope that this EDUCBA information on “SED Command in Unix” was beneficial to you. You can view EDUCBA’s recommended articles for more information.