Embed
Email

sed and awk

Document Sample

Shared by: dandanhuanghuang
Categories
Tags
Stats
views:
1
posted:
11/22/2011
language:
English
pages:
7
sed and awk



Henning Schulzrinne

Dept. of Computer Science

Columbia University



22-Nov-11 Advanced Programming

Spring 2002

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





22-Nov-11 Advanced Programming 2

Spring 2002

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

22-Nov-11 Advanced Programming 3

Spring 2002

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

22-Nov-11 Advanced Programming 4

Spring 2002

awk

 Patterns = boolean combinations of regular

expressions and relational expressions

 awk –f program 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 }



22-Nov-11 Advanced Programming 5

Spring 2002

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}

22-Nov-11 Advanced Programming 6

Spring 2002

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









22-Nov-11 Advanced Programming 7

Spring 2002



Related docs
Other docs by dandanhuanghua...
južna amerika
Views: 2  |  Downloads: 0
stab2
Views: 0  |  Downloads: 0
Download File - Weebly
Views: 0  |  Downloads: 0
intro_cw
Views: 0  |  Downloads: 0
Info on Portsmouth race scoring
Views: 0  |  Downloads: 0
PMOS Instructions update
Views: 0  |  Downloads: 0
Respirator Standard Photos_1_
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!