sed

Name

sed - stream editor for filtering and transforming text

Purpose

Edit streams of text allowing entire text files to be filtered, or transformed in any number of ways.

Usage

  • 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 sed will 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

Substitute Flags

  • N: Only the Nth matched pattern of the line should be replaced
  • g: All matched patterns of the line should be replaced
Note

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.

Setup

This and the following articles use the user-data.csv file.

If you don’t already have the user-data.csv file in your home directory run the following command:

curl -s https://launchcodelearning.org/api/walkthrough/user?data_format=csv > ~/user-data.csv
Bonus

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 'a' with 'q'

Warning

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:

cd

Replace the first a in each line with q:

sed 's/a/q/1' user-data.csv

Output:

sed ’s/a/q/1’ user-data.csv output

Looking at the last record displayed in STDOUT:

Kristy,Strong,[email protected],Hunter Engineering

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 q character.

Note

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 'a' with 'q'

Replace all occurrences of a with q:

sed 's/a/q/g' user-data.csv

Output:

sed ’s/a/q/g’ user-data.csv output

The last record: Kristy,Strong,[email protected],Hunter Engineering was changed to: Kristy,Strong,[email protected],Hunter Engineering