Version 12 of sed

Updated 2006-09-05 13:53:06 by suchenwi

Stream Editor

A program that parses files or standard input, changes it according to a given set of rules, mostly based on Regular Expressions, and sends the new data to standard output. Unbelievably fast.

Erik Pemente keeps a very good amount of info and links in this page: http://www.student.northpark.edu/pemente/sed/ A spirited introduction to sed programming appears here [L1 ].

There is a page dedicated to the Tcl's implementation of Regular Expressions on this wiki. Tcl uses ARE. sed uses Basic and Extended Regular Expressions only.

One reason Tcl'ers encounter sed is that autoconf and autoconf-generated build scripts makes heavy use of sed to mangle text.


 $ sed s/foo/bar/ < infile > outfile

can in Tcl be substituted either with regsub or, when like in this case, no regular expression features are needed, with string map:

 % > outfile [regsub -all foo [< infile] bar]

resp.

 % > outfile [string map {foo bar} [< infile]]

where

 proc > {filename str} {set f [open $filename w]; puts $f $str; close $w}
 proc < {filename}     {set f [open $filename]; return [read $f][close $f]}

RS 2006-09-05: Playing around, waiting for a longish run to complete: proc sed {script input} {

    set sep [string index $script 1]
    foreach {cmd from to flag} [split $script $sep] break
    if {$cmd ne "s"} {error "not yet implemented"}
    set cmd regsub
    if {$flag eq "g"} {lappend cmd -all}
    lappend cmd $from $input $to
    eval $cmd

} Testing:

 sed s-foo-bar "A foolish idea for fools"
 A barlish idea for fools
 % sed s/foo/bar/g "A foolish idea for fools"
 A barlish idea for barls

Add to it to make it come closer to the real thing:^)


[ Category Acronym | Category Application ]