Manipulate Text in Files With Sed
The sed linux utility is a powerful stream editor. Sed can manipulate text from standard input or from a file. It is much like a typical text editor. Where sed sets itself apart is in its ability to filter text in a pipeline to expand its capabilities.
Sed can work wonders in a shell script. You can pipe other commands to sed or use variables. Use it to process files of all sizes. Find specific strings of text and patterns in your input and manipulate the text to your needs. With sed you can also edit a file directly or save to a new file. The possibilities with sed are near endless.
In this tutorial I will show you some basic and common uses of sed to manipulate text.
Manipulate Text With Sed
Basic sed syntax.
sed [options] [command] [file-to-eidt]
Sed has an extensive list of options to manipulate input. For this tutorial we will be using 2 options.
- [-e] Doesn’t edit the file in place, but rather shows the edited output in your terminal. This is good for testing before actually editing a file directly.
- [-i] Edits a file in place. Can also make a backup if you add a suffix. Example: [-i-bak]
Create a file called “test” and add the following sentence to it.
We will learn how to use sed to edit text input streams.
We will use the above sentence to practice with sed.
Replace a string with another string.
In this first example we will replace the word “learn” with “follow”. We will first run a test with the [-e] option just to be sure before making the final edit.
sed -e 's/learn/follow/g' test
You should see the word learn replaced with follow in your terminal output. Now lets make the edit directly to the file with the [-i] option.
sed -i 's/learn/follow/g' test
If you open the test file you’ll see the word learn replaced with follow. Now lets change it back to learn, only this time we will make a backup of the original file.
sed -i-bak 's/follow/learn/g' test
As you can see a backup file called “test-bak” was created and then the original file was edited. Lets instead use sed to create a new file called “my-file” with our changes.
sed 's/follow/learn/g' test > my-file
You should now have the new file with the word follow replaced with learn while the original test file still shows the word follow.
Now that you’ve learned a few basic examples with sed we can move at a faster pace through the tutorial.
More Sed Examples
Delete all empty lines with sed.
If you have a file with multiple lines separated by empty lines you can remove the empty lines easily with sed.
sed -i '/^\s*$/d' filename
Delete a word or phrase.
Delete every instance of a word or phrase.
sed -i 's/word-to-remove//g' filename
Append to the beginning of every line.
Add a word or phrase to the beginning of each line in a file.
sed -i 's/^/word-or-phrase/' filename
Append to the end of every line.
Add a word or phrase to the end of each line in a file.
sed -i 's/$/words or patterns/' filename
Convert all file contents to uppercase.
Convert every letter in a file to uppercase.
sed -i 's/\(.*\)/\U\1/' filename
Convert all file contents to lowercase.
Convert every letter in a file to lowercase.
sed -i 's/\(.*\)/\L\1/' filename
Conclusion
Sed is an extensive and powerful text stream editor and we only scratched the surface on what it can do. If you’d like to learn more about sed, have a look at the manual. Just run this command in terminal.
man sed
For more sed commands, check out my other sed tutorial.
Tags: linux, tutorials, command line