Programming patterns in sed

Philip S Tellis

Yahoo! Inc

Opensource Bridge 2009

$ whoami

flickr:code_martial/1543735477

Stream EDiting

Stream EDiting

Stream EDiting

?

Stream EDiting

Stream EDiting

Stream EDiting

Stream EDiting

Stream EDiting

Stream EDiting

Enough history, where be dem Patterns???

Prerequisites

Is it an editor or a programming language?

flickr:mamk/2377536817

Is it an editor or a programming language?

flickr:mamk/2377536817

Is it an editor or a programming language?

flickr:mamk/2377536817

Is it an editor or a programming language?

flickr:mamk/2377536817

To write complex programs, all you need is if and goto

A structured programming language

flickr:adobemac/2895835834

A useful structured programming language

flickr:adobemac/2895835834

1Sequence

sed scripts flow sequentially from top to bottom unless a branch is involved

1Sequence

sed scripts flow sequentially from top to bottom unless a branch is involved

2Selection

flickr:kt/1118569929

2Selection - if some condition, do some thing

flickr:kt/1118569929

   /pattern/ command

2Selection - if some condition, do some thing

flickr:kt/1118569929

   /pattern/ command

   s/pattern/replace/
   t label

   s/pattern/replace/
   T label

2Selection

flickr:kt/1118569929

   /^hello/ s/^hello/hello world/

   3 p

   /^next\>/ {
      N
      s/\(.*\)\n\(.*\)/\2\n\1/
   }

2Selection - Sample input

flickr:kt/1118569929

   hello
   my name is sed
   print this line out twice
   next line first
   this is line #5

2Selection - Sample output

flickr:kt/1118569929

   hello world
   my name is sed
   print this line out twice
   print this line out twice
   this is line #5
   next line first

2Selection

flickr:kt/1118569929

   /^hello/ s/^hello/hello world/

   3 p

   /^next\>/ {
      N
      s/\(.*\)\n\(.*\)/\2\n\1/
   }

2Selection

For readability, use a condition followed by a code block:
/condition/ {
command1
command2
command3
}

3Iteration/Loops

flickr:bluesmoon/241327100

3aIteration - Entry controlled

flickr:bluesmoon/241327100

   :loopstart
   /condition/ {
      command1
      command2
      command3
      b loopstart
   }

3aIteration - Entry controlled

flickr:bluesmoon/241327100

   :loopstart
   /==/ {
      s/==//
      r equals.txt
      b loopstart
   }

3bIteration - Exit controlled

flickr:bluesmoon/241327100

   :loopstart
      command1
      command2
      command3
   /condition/ b loopstart

3bIteration - Exit controlled

flickr:bluesmoon/241327100

   :loopstart
      r equals.txt
   /==/ b loopstart

3bIteration - Exit controlled

flickr:bluesmoon/241327100

   :loopstart
      r equals.txt
      s/==//
   t loopstart

3bIteration - Exit controlled

flickr:bluesmoon/241327100

   :loopstart
      r equals.txt
      s/==//
   T loopstart

3cIteration - fixed counter

flickr:bluesmoon/241327100

3cIteration -fixed counter

flickr:bluesmoon/241327100

   # Print the current line 10 times:

   # 1. Grab the current line into the hold space
   h
   # 2. Replace the pattern space with x based on what we want to count to
   c xxxxxxxxxx
   # 3. Print the line as long as there are x left:
   :loopstart
      s/^x//
      T loopend
      x
      p
      x
   b loopstart

3cIteration - fixed counter

flickr:bluesmoon/241327100

Avoid fixed counter for loops in sed. They can most often be replaced with an entry controlled loop over real data or a more general purpose programming language

4Variables

4Variables

4Variables

4Variables

4Variables

4Variables

4Variables

   # 1. Swap the hold and pattern space
   x
   # 2. Set the pattern space to the value of your variable
   #    using the s, c, i, a, g or G commands:
   s/$/\nfoo\n/
   G
   # 3. Swap the hold and pattern space again
   x

4Variables - key/value pairs

   x
   s/$/\nname:/
   G
   s/name:\n/name:/
   s/$/\n/
   x

  variables1.sed

5Debugging

5Debugging

   =
   l
   x
   l
   x

   variables2.sed

6File handling

6File handling - example

Gaming

Photo Credits

flickr:25602112@N07/2539754489

Links

Thank you

Made with Eric A Meyer's S5