CIS 218 stream editor (sed) CIS 218 Advanced UNIX 1 sed Uses - - PowerPoint PPT Presentation

cis 218 stream editor sed
SMART_READER_LITE
LIVE PREVIEW

CIS 218 stream editor (sed) CIS 218 Advanced UNIX 1 sed Uses - - PowerPoint PPT Presentation

CIS 218 stream editor (sed) CIS 218 Advanced UNIX 1 sed Uses same syntax as vi Batch front end to same ed command Works record by record thru entire file sed [-n] script [filelist] or sed [-n] -f scriptfile [filelist]


slide-1
SLIDE 1

CIS 218 Advanced UNIX 1

CIS 218 stream editor (sed)

slide-2
SLIDE 2

CIS 218 Advanced UNIX 2

sed

  • Uses same syntax as vi
  • Batch front end to same ed command
  • Works record by record thru entire file
  • sed [-n] script [filelist]
  • r
  • sed [-n] -f scriptfile [filelist]
  • if no filelist is given, it is often used in a pipe
  • script = one or more lines in the following format:

[address[,address]] instruction [argument list]

slide-3
SLIDE 3

CIS 218 Advanced UNIX 3

sed

  • Addressing can be the same as we saw in vi, but it can also be regular expressions

to match instructions can be any one from the list:

  • d - delete
  • n - next
  • a - append
  • i - insert
  • c - change
  • s - substitute
  • p - print
  • w - write
  • r - read
  • q – quit
  • to modify the instructions you can also use

– ! - not - to mean if it does not match – { } - to group the instructions together on a single match

  • the -n says don't print a line to the output unless specifically instructed to. Default

is to print all lines.

  • s (substitute) is the same command specified in vi and regular expressions: s/old/new/[g]
slide-4
SLIDE 4

CIS 218 Advanced UNIX 4

sed Examples

  • % sed '3,6 p' file - print all lines, duplicates lines 3, 4, 5,

and 6 in the output

  • % sed -n '3,6 p' file - will print only the lines 3 thru 6

to the output

  • % sed -n '/line/ p' file - will print any lines that contain

the string "line“

  • sed commands often put into a sed-script file:

cat pscript 3,6 p

  • sed -n -f pscript file will print the lines 3thru 6 to output
  • r used in a pipe: who | sed -n -f pscript | more
slide-5
SLIDE 5

CIS 218 Advanced UNIX 5

Sed remembering what was found

  • Things enclosed in the \( \) pair will be stored (up to 9) to be used as \1 , \2 ...

\9

  • % cat rev.script

s/^\([0-9]\)\([0-9]\)/\2and\1/

  • % cat num.file

23 Jan 43 Pete 72 Fred 91 Mike

  • % sed -f rev.script numfile

32 Jan 34 Pete 27 Fred 19 Mike