Chapter 13 sed
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

Chapter 13: sed PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

Chapter 13: sed. Say what?. In this chapter …. Basics Programs Addresses Instructions Control Spaces Examples. sed. GNU sed (stream editor) Noninteractive, batch editing Good for repetitive tasks Often used in a pipe. sed syntax. Syntax: sed [ options ] program [ filelist ]

Download Presentation

Chapter 13: sed

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Chapter 13 sed

Chapter 13:sed

Say what?

In this chapter

In this chapter …

  • Basics

  • Programs

  • Addresses

  • Instructions

  • Control

  • Spaces

  • Examples

Chapter 13 sed


  • GNU sed (stream editor)

  • Noninteractive, batch editing

  • Good for repetitive tasks

  • Often used in a pipe

Sed syntax

sed syntax

  • Syntax:

    sed [options] program [filelist]

    sed [options] program-file [filelist]

  • Program is a set of commands for editing

    • Can either be issued on the command line or placed into a file (like gawk)

  • Filelist is a list files to edit

    • If omitted, input taken from standard in

Sed syntax con t

sed syntax con’t

  • Options


    • Instead of sending edited text to standard out, write changes back to input file

    • Adding =suffix makes backup of original file


    • Do not send lines to output unless program explicitly says to



  • sed programs contain one or more lines with the following syntax:

    [address[,address]] instruction [args]

  • Simple one or two line programs can be issued at the command line

  • More complex programs are usually best put in a program file

How sed works

How sed works

  • Read one line of input

  • Read first instruction in program. If the address(es) select this line, runs the instruction on this line

  • Repeat #2 for each line in the program

  • Read next line of input and go back to step 2, until there are no more lines of input



  • Select which lines are to be processed

  • Can be a simple integer (line number) or a regular expression (pattern matching)

  • Address $ represents last line of input

  • If address omitted, all lines processed by default

  • If there is one address, only lines that match will be processed

Addresses con t

Addresses con’t

  • If two addresses are given, it selects a range

  • Once the first address is matched, it and subsequent lines are processed until the second address is matched

  • If second address is never matched, processes remainder of lines

  • If second addressed matched, sed will then try to match first address again



  • d – does not write out (deletes) selected line and does not process line any further

  • n – writes out current line, reads next line, and processes next program line

  • a – appends lines after current line

  • i – inserts lines before current line

  • c – changes select line so it contains new text

  • p – print current line (override –n)

Instructions con t

Instructions con’t

  • w file – write line to a specified file

  • r file – read contents of file and appends to current line

  • q – quits sed immediately

Instructions con t1

Instructions con’t

  • s/pattern/replacement-str/[g][p][w file]

    • Substitutes first occurrence of pattern with replacement-str

    • g replaces all occurences

    • p prints changed line

    • w writes changed line to file

  • Use & to represent the pattern matched when replacing

    • Ex. s/a.*/(a.*)/ won’t work … instead use s/a.*/(&)/

Control structures

Control Structures

  • ! (NOT) – causes instruction to be performed on all lines not selected by address(es)

  • { } (Instruction grouping) – causes multiple instructions to be run on one address / address pair; separate with semicolons

  • : label – identify a location in a sed program

  • b label – branch to label

  • t label – conditionally branch to label if last Substitute instruction was successful



  • sed has two spaces (buffers)

  • Think of them like vim’s buffers

  • Lines read from input are put in patternspace

  • You can also move data back and forth from the hold space (temporary buffer)

Spaces cont

Spaces, cont

  • g – overwrites pattern space with hold space

  • G – appends hold space to pattern space

  • h – overwrites hold space with pattern space

  • H – appends pattern space to hold space

  • x – swaps the pattern and hold spaces



  • sed -n‘/line/ p’ myfile

    • Prints out lines in myfile that contain ‘line’

  • sed ‘2,4 d’ myfile

    • Delete lines 2-4, outputs remaining

  • sed --in-place ‘2,4 d’ myfile

    • Deletes lines 2-4 from myfile

  • sed ‘s/tea/coffee/g’ myfile

    • Replaces tea with coffee and prints to screen

More examples

More Examples

  • sed ‘5 q’ myfile

    • Prints first five lines then quits ( equiv. head -5)

  • sed ‘/^[0-9]/ w newfile’ myfile

    • Copies lines starting in number to newfile

  • sed ‘$ r newfile’ myfile

    • Appends contents of newfile to end of myfile

  • sed ‘G’ myfile

    • What does this do?

Program file example

Program File Example

1 d


$ a\

Revised 12-1-2005\

by JMH

$ d

Another program file

Another Program File

1 i \





s/.*/ &/

$ a \


  • Login