sed

sed, the Stream Editor is a text editor that performs editing operations on information coming from standard input or a file.

 

sed (non-interactively) edits its input line-by-line, operates on it, and outputs the resulting text before repeating the process on the next line.

 

Basic usage:

  • sed [options] command [targetFile]

 

sed sends it results to screen by default and can therefore be used to display a file's contents:

  • sed '' targetFile.txt
    • in the above example there's no command being sent in the single quotes, so it simply printed each line it received to stdout
  • cat targetFile.txt | sed ''
    • sed can use stdin by piping the output of cat for the same result

 

 

Printing lines

Using sed's explicit "print" command, specified by the 'p' character within single quotes.

  • sed 'p' targetFile.txt
    • notice each line is now printed twice, due to it automatically printing each line and plus we've told it to print explicitly with the 'p' command

The -n option can be used to suppress (the default) printing:

  • sed -n 'p' targetFile.txt
    • so it now only prints each line once

 

 

Address Ranges

Print just the first line:

  • sed -n '1p' targetFile.txt

 

Print five lines:

  • sed -n '1,5p' targetFile.txt
    • this is known as an address range
  • sed -n '1,+4p' targetFile.txt
    • alternative syntax

Print every other line:

  • sed -n '1~2p' targetFile.txt

 

 

Deleting text

Delete every other line:

  • sed '1~2d' targetFile.txt

Send output to a file:

  • sed '1~2d' targetFile.txt > newFile.txt

 

By default, sed doesn't edit the source file. However this can be overridden by providing the -i option to perform edits in place, to edit the source file:

  • sed -i '1~2d' targetFile.txt

This can be dangerous, but sed allows a backup to be created prior to editing:

  • sed -i.bak '1~2d' targetFile.txt
    • above creates a backup file with the .bak extension

 

 

Substituting text

  • sed s/search/replace/
    • the initial s specifies the action to be performed: substitute
    • the pattern being searched for is placed between the first and second /
    • the replacement pattern is between the second and third /

 

Other characters can also be used as delimiters, which come in handy if / is within the search or replace patterns:

  • s_search_replace_
  • echo "https://www.example.com/index.html" | sed 's_com/index_org/home_'
    • https://www.example.org/home.html

 

sed substitutes the first instance it finds of the pattern, line by line! To replace all occurrences, use the g flag after the substitution set:

  • sed s/search/replace/g
    • the g specifies a global replacement, otherwise it would only replace the first pattern match

 

If we only wanted to change the second instance of the search pattern that sed finds on each line, then we could use the number "2" instead of the "g":

  • sed s/search/replace/2

 

More complex patterns can be found by using regular expressions. For instance, if we want to match the from the beginning of the line to "at" we can use the expression:

  • sed 's/^.*at/REPLACED/' targetFile.txt

 

 

some further examples