SED Quick Reference Guide

Introduction

what_is_sed.jpeg

 

Format

sed options script file

The options parameters allow you have more control over the behavior of sed

Below are some popular ones (find more with man sed:

Option Description
-e script add the script to the commands to be executed
-f file add the contents of script-file to the commands to be executed
-n  suppress automatic printing of pattern space

The script parameter specifies a single command to apply against the stream data. If more than one command is required, you must use either the -e option to specify them in the command line or the -f option to specify them in a separate file. Numerous commands are available for manipulating data. ~ Linux Command Line & Shell Scripting Bible

Flags

The substitute command (the initial s used in script. ex: "sed 's/dog/cat/' data1.txt) only works on the first occurrence in each line. This can be fixed with any of the 4 flags of the substitute command.

s/pattern/replacement/flags

Four types are available:

  • -- a number, indicating the pattern occurrence for which new text should be substituted
  • -- indicating that new text should be substituted for all occurrences of the existing text
  • p -- indicating that the contents of the original line should be printed
  • file -- write the results of substitution to a file

Check out the Quick References on this page for examples.

Quick References

The file data0.txt will hold the string "This is a test of the test script"
The file data1.txt will hold the string "The quick brown fox jumped over the lazy dog"
The file data2.txt will hold 5 lines of: 
  This is line #1
  This is line #2
  This is line #3
  This is line #4
  This is line #5

 

Example Description
sed -n '1p' data1.txt Prints only line 1
sed 's/dog/cat/' data1.txt replace the first occurrence of dog with cat on every line
sed -e 's/brown/green/; s/dog/cat' data1.txt replace the first occurrence of brown with green, and dog with cat on every line.
The -e  is used to string commands together
sed -f sedfile.sed data1.txt The -f uses the sedFile to manipulate data1.txt
sed 's/test/data0/2' data0.txt This will replace the 2nd occurrence of test with data0
sed 's/test/data0/g' data0.txt This will replace all the occurrences of test with data0
sed -n 's/#1/#0/p' data2.txt This pairs -n option with the p flag. The -n  suppresses all output and the p flag only prints the changed lines. 
sed 's/test/trial/w test.txt' data3.txt This uses the w flag to write data to data3.txt

sed '2s/dog/cat/' data1.txt

sed '2,3s/dog/cat/' data1.txt

sed '2,$s/dog/cat/' data1.txt

The 2s only substitutes the text in line 2.

This can also to isolate multiple lines separated by a comma.

The $ symbolizes last line. This command impacts lines 2 through the last line

sed '/tree/s/bash/zsh/' /etc/passwd The pattern/command syntax allows you to make changes only to that pattern.
sed '3i\
This is an inserted line.' data1.txt

The 3i inserts the line below it before the 3rd line.

Using the back slash, and placing the inserting text on a new line is important to this working properly.

sed '3a\
This is an appended line.' data1.txt

The 3aappends the line below it after the 3rd line.

Using the back slash, and placing the inserting text on a new line is important to this working properly.

sed '1i\
This is one line of new text.\
This is another line of new text.' data2.txt

This would insert both lines before the first line.

sed '3c\
This is a changed line of text.' data2.txt

The 3c changes the entire 3rd line.

sed '/#3/c\
This is a changed line of text.' data2.txt

You can use pattern/command  as well, this will replace entire line matching #3 with the the followed string.

sed '=' data2.txt

This will print the line number of each line.
sed determines a new line from the '\n' character in the data stream.

sed '1,2w data0.txt' data3.txt

The filename can be specified as either full or relative pathname.(must have permissions for file).
This will only write lines 1 and 2 to data3.txt file, but still print out entire data0.txt

sed '3r data2.txt' data7.txt

 

The 3r appends all the data from data2.txt after the 3rd line in data7.txt.

This works with pattern matching as well. using the $r will just append to the end.