Sed and awk
Download
1 / 7

- PowerPoint PPT Presentation


  • 189 Views
  • Updated On :

sed and awk. Henning Schulzrinne Dept. of Computer Science Columbia University. sed: string replacement. Line-oriented tool for pattern matching and replacement (stream editor) Not really a programming language (cf. awk) E.g., apply same change to lots of source files

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '' - kiona


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
Sed and awk l.jpg

sed and awk

Henning Schulzrinne

Dept. of Computer Science

Columbia University

Advanced Programming

Spring 2002


Sed string replacement l.jpg
sed: string replacement

  • Line-oriented tool for pattern matching and replacement (stream editor)

  • Not really a programming language (cf. awk)

  • E.g., apply same change to lots of source files

  • Filter, i.e., does not modify input file

Advanced Programming

Spring 2002


Sed description l.jpg
sed description

  • pattern a text  add to output

  • address s /regex/replacement/

  • address d  delete line

  • delete lines 1-10: sed -e '1,10d‘

  • delete comments: sed -e '/^#/d‘

  • print only matching: sed -n -e '/regexp/p

  • convert Unix to DOS: sed -e 's/$/\r/' myunix.txt > mydos.txt

Advanced Programming

Spring 2002


Slide4 l.jpg
awk

  • Special-purpose language for line-oriented pattern processing

  • pattern {action}

  • action =

    • if (conditional) statement else statement

    • while (conditional) statement

    • break

    • continue

    • variable=expression

    • print expression-list

Advanced Programming

Spring 2002


Slide5 l.jpg
awk

  • Patterns = boolean combinations of regular expressions and relational expressions

  • awk –f program < input > output

  • Also delimiter-separated fields:

    BEGIN {FS=c}

  • Examples:

    • Print lines longer than 72 characters:

      length > 72

    • print first two fields in opposite order

      { print $2,$1 }

Advanced Programming

Spring 2002


Awk examples l.jpg
awk examples

  • Add up first column, print sum and average

    {s += $1 }

    END {print “sum is”, s, “average is”, s/NR}

  • Print all lines between start/stop words:

    /start/,/stop/

  • Print all lines whose first field differs from previous one:

    $1 != prev {print; prev = $1}

Advanced Programming

Spring 2002


Awk applications l.jpg
awk applications

  • Unix file processing, e.g., as part of a pipe

  • avoid read loop in shells, Perl

  • more intuitive syntax than shell scripts

  • best limited to line-at-a-time processing

Advanced Programming

Spring 2002


ad