Updated April 17, 2023
Introduction to SED Command in Linux
The following article provides an outline for SED Command in Linux. SED as text manipulation tool that we use regularly and we think a lot of shell script is use regularly and SED stands for stream editor and basically allows you to manipulate text files substituting, replacing, searching, inserting, deleting without opening the files. Moreover, SED enables us to use a regular expression to match the expression within the file and perform text manipulation.
Syntax:
sed OPTIONS [SCRIPT] INPUTFILE
Some options available are:
- -n, –quiet, –silent: Suppress automatic printing of pattern space.
- -f script-file, –file=script-file: Add script file.
- -e script, –expression=script: Add the script to the commands to be executed.
- -l N, –line-length=N: Specify the desired line-wrap length, N, for the “l” command.
Examples of SED Command in Linux
Given below are the examples of SED Command in Linux:
Example #1
Code:
cat test|sed 's/t/T'
Output:
Explanation:
- We’re going to say substitute forward slash forward slash forward slash. We’re going to give in between the first and second forward slash what we want to replace, what string we want to replace, and the second one what we’re going to replace it with, so for example, I can say T here and then here I can put an uppercase T because we’re gonna be replacing the lowercase T’s with uppercase T’s if I hit enter at this point we’ll see the lines from the text file. Still, we have only modified the first T in each line to be capital; you’ll notice that the second T and the third T and the other T on that line doesn’t work, so basically, at this point, we haven’t modified the original file.
Example #2
Code:
sed 's/t/T/g text
Output:
Explanation:
- G, which stands for global, meaning find all the lowercase T’s and transform them, so there we go that’s what we’ve got now we’ve got every T is now an uppercase T on every line again we have not modified the actual file yet.
Example #3
Code:
sed 's/t/T/g text>new.txt
Explanation:
- We can create a new file by redirecting into it so we can take the output of this and put it into new.txt or whatever you want to call it, so now we can cat out new.txt.
Example #4
Code:
sed -i 's/t/T/g text
Cat text
Output:
Explanation:
- The original file is now changed.
Example #5
Code:
sed 's/^t/oooo/g text
Output:
Explanation:
- So let’s say I just wanted to replace the T at the beat that starts at the beginning of each line, so that would be this and tree are both beginning the lines, so we’re looking for a T at the beginning of the line what we can do here is we can give it the caret symbol which on a standard keyboard most no difficult of different keyboards but it’s over the six on most keyboards. So now it’s saying this caret indicates the beginning of lines, so it’s saying we’re looking for the beginning in line T. Hence, it’s still kind of like it’s part of the string you know it’s we’re and what we’re going to do is we’re going to replace it with oooo so now I’ll hit enter once again we’re not we don’t have our – I in here so we do not modify the original file we’re just displaying it differently we’re going to enter here. You can see that not all of our T’s have been replaced but only the T’s that are at the beginning of each line.
Example #6
Code:
sed 's/t$/oooo/g text
Explanation:
- We can do the same thing with the end of a line now the caret symbol indicates the beginning of the line well a dollar sign indicates the end of a line, so this is going to find every line that ends with a T because it’s going to be T and then the end of the line and we’re going to replace that with oooo. I’ll hit enter, and you can see we get the T right there; oh, and we can do it with ease as well; in this case, we’ll or any string. Still, we have three the ends and E and twice the ends in E, and they’re both at the end aligned we’d enter, and you can see we’ve got o and O there, and of course, if we did two E’s it would only affect the three because there’s only one e and twice and then the end of the line.
Example #7
Code:
sed 's/[0-9]/*/g text
Output:
Explanation:
- We’re looking for, I can say inside square brackets here 0-9 meaning look at all numeric characters, and I can say replace every numeric character, so we’re not looking at alpha characters are not looking at the letters in the alphabet we’re looking at numbers and replace them with a strict and that’s within the text file again there’s no – I hear someone up modifying the file just changing the output, and you can see everywhere that there was a number there is now an asterisk now I can do the same thing with a dash Z.
Example #8
Code:
sed 's/[a-zA-Z]/*/g' text
Output:
Explanation:
- Searching [a-zA-Z] and replacing with *.
Example #9
Code:
sed 's/[A-Z]i/*/g' text
Output:
Explanation:
- We’ll say replace all capital letters that are followed by an I, so here we it replaced the T and the I because it’s looking for both those things and replacing it with that it’s not looking for one or the other it’s looking for the two together and the only place where an eye follows a capital letter was the word tiny.
Conclusion
We have seen step by step definition and example using the SED command about replacing string and finding and replacing or redirecting the result to a new file or modifying the same file.
Recommended Articles
We hope that this EDUCBA information on “SED Command in Linux” was beneficial to you. You can view EDUCBA’s recommended articles for more information.