sed - stream editor for filtering and transforming text
Edit streams of text allowing entire text files to be filtered, or transformed in any number of ways.
- search and replace
- add content before or after specific patterns
- delete lines
- add lines
Like many of the Userspace Applications introduced in this course we will only be covering a small portion of what you can do with the tool.
In the case of
sed we will simply be showing you how to use the substitute feature. You have likely used a similar feature in other software called
Find & Replace.
sed Command Pattern
sed '[script]' 'input-file'
sed Substitute Syntax
sed 's/regex-pattern/replacement-text/flags' 'input-file'
s: the action
sedwill be performing, this case a substitute action
regex-pattern: a regular expression sed should search each line for
replacement-text: the text to replace the regular expression with
flags: which matching section(s) should be replaced
N: Only the Nth matched pattern of the line should be replaced
g: All matched patterns of the line should be replaced
If you do not include a substitute flag it will default to
1, so only the first matched pattern on the line will be replaced.
This and the following articles use the
If you don’t already have the
user-data.csv file in your home directory run the following command:
curl -s https://launchcodetechnicaltraining.org/api/walkthrough/user?data_format=csv > ~/user-data.csv
You can validate the
user-data.csv file with the following command:
wc -l ~/user-data.csv
The output you see should confirm there are 25001 lines in the file.
sed substitute First Occurrence of
All commands in this and following articles assume your current working directory is the directory where the
user-data.csv file resides, most likely your home directory.
You can change into your home directory from anywhere with the command:
Replace the first
a in each line with
sed 's/a/q/1' user-data.csv
Looking at the last record displayed in
It looks like
nwilliams was changed to
nwilliqms. Take a look at the other lines to notice that the first instance of each
a character was replaced with a
STDOUT shows the substitution that was made, however
sed does not edit the original file by default. You would need to instruct
sed to save the changes by writing STDOUT to a file with the redirection operator (
>) or use the
-i option. Both of these options for saving changes will be covered in future sections.
sed substitute All Occurrences of
Replace all occurrences of
sed 's/a/q/g' user-data.csv
The last record:
Kristy,Strong,email@example.com,Hunter Engineering was changed to: