SED is a stream editor that is used to filter and transform text. The SED command can be used to match patterns, and replace one piece of text with another amongst other things. SED is indeed a powerful text manipulation command. In this tutorial, we will be learning about the various uses of the sed command.
You may also like:
When piping isn’t used, one can use sed with a file as input. In such cases, the input file is INPUTFILE.
sed SCRIPT INPUTFILE
sed ‘s/world/hacker/’ file.txt > output.txt
In the previous piece of code, what we’re doing is taking the word world, and substituting it with hacker in the file called file.txt and saving it as output.txt.
Alternatively, we could have written the the previous code as follows:
cat file.txt | sed ‘s/world/hacker/’ > output.txt
The s command
One can substitute or replace a piece of text with another using the s command.
Here “regexp” is the text that it will search for, and if a match is found, then replace it with the “replacement” string.
echo “hello world” | sed 's/world/hacker/'
The output for the following would be “hello hacker”.
By using the s command, we can substitute a word for another, but suppose that we want to substitute let’s say lines 1 through 5 with “hello world”, then we’d write:
seq 10 | sed “1,5c hello world”
The result would be as follows:
hello world 6 7 8 9 10
The y command
The y command in sed is used to transliterate characters.
echo this is sed | sed ‘y/abcdefghi/012345678/’
The result of the latter would be:
T78s 8s s43
echo 1234567 | sed ‘y/123/sed/’ sed4567
One can also pass multiple sed commands in one line using a semi colon.
sed “s/this/This/; s/line/verse/;” file.txt
The latter will perform both commands on file.txt and chuck out the results in the terminal. Mind you, this doesn’t change the original file.
Typically, if a substitution is carried out using sed, it will substitute the first variable only. In such cases, we need the “global’ variable.
echo “hello world, the world is a great place to live in. ~ world” | sed 's/world/hacker/'
Result for ex1:
hello hacker, the world is a great place to live in. ~ world.
Now suppose that we add the global variable g. Here we would have s/regexp/replacement/g.
echo “hello world, the world is a great place to live in. ~ world” | sed s/world/hacker/g
Result for ex2:
hello hacker, the hacker is a great place to live in. ~ hacker.
In addition to replacing texts, and pattern matching. Sed allows one to modify texts by inserting or deleting items.
For example, you can delete lines 1 through 3 in a file called file.txt by writing the following code:
sed ‘1,3d’ file.txt > output.txt
Ok, so we can delete, but we can also insert lines:
sed '3 i\ This was inserted before the third line' file.txt
The latter will insert the line “this was inserted before the third line” right before the third line.
In order to append the line after the line and not before, we write:
cat file2.txt | sed ‘2a this is a new line’
The latter will insert the line “this is a new line” after the second line.
However, please remember that without outputting the contents to a new file or using the -i switch, the change is only visible in the terminal, not in the actual file. What this means is that the contents of file.txt were not modified.
Instead of passing sed commands in the terminal, we can always dump them in a script file, and use the script file to execute the sed commands.
echo “s/line/verse/” > script.sed echo “s/this/This/” >> script.sed echo “3 i this is a line” >> script.sed sed -f script.sed file.txt
The latter will create a script.sed file with 3 different commands in it. These commands are then activated using the -f switch, and the input file.
SED is a stream editor with many a function. It can be used to filter and manipulate texts. Although it a very very powerful tool with very many functions, in this tutorial, we covered only the basics of sed.
What is sed?
SED is a stream editor that is used to filter and transform text. The SED command can be used to match patterns, and replace one piece of text with another amongst other things.
What does the s command in sed do?
Using sed, one can substitute or replace a piece of text with another using the s command.
Here, s is for substitute, and “regexp” is the text that it will search for, and if a match is found, then replace it with the “replacement” string.
Can we use scripts in the sed command?
Yes. Instead of passing sed commands in the terminal, we can always dump them in a script file, and use the script file to execute the sed commands using the -f switch.
Can we insert or delete texts using sed?
Yes. In addition to replacing texts, and pattern matching. Sed allows one to modify texts by inserting or deleting items. To insert items, you can either insert them before or after a particular line.
How do we susbstitute all values in a file?
In order to substitute all values in a file with another value, you will need the global or g value.
sed ‘s/regexp/replacement/g’ INPUTFILE
ex: sed ‘s/hello/world/g’ file.txt