chapter 13: sed say what?. in this chapter … basics programs addresses instructions control spaces...

19
Chapter 13: sed Say what?

Upload: merryl-clare-rodgers

Post on 03-Jan-2016

225 views

Category:

Documents


8 download

TRANSCRIPT

Chapter 13:sed

Say what?

In this chapter …• Basics

• Programs

• Addresses

• Instructions

• Control

• Spaces

• Examples

sed• GNU sed (stream editor)

• Noninteractive, batch editing

• Good for repetitive tasks

• Often used in a pipe

sed syntax• Syntax:

sed [options] program [filelist]

sed [options] program-file [filelist]

• Program is a set of commands for editing– Can either be issued on the command line or

placed into a file (like gawk)

• Filelist is a list files to edit– If omitted, input taken from standard in

sed syntax con’t• Options

--in-place[=suffix]• Instead of sending edited text to standard out, write

changes back to input file• Adding =suffix makes backup of original file

-n• Do not send lines to output unless program explicitly

says to

Programs• sed programs contain one or more lines with

the following syntax:

[address[,address]] instruction [args]

• Simple one or two line programs can be issued at the command line

• More complex programs are usually best put in a program file

How sed works1. Read one line of input

2. Read first instruction in program. If the address(es) select this line, runs the instruction on this line

3. Repeat #2 for each line in the program

4. Read next line of input and go back to step 2, until there are no more lines of input

Addresses• Select which lines are to be processed

• Can be a simple integer (line number) or a regular expression (pattern matching)

• Address $ represents last line of input

• If address omitted, all lines processed by default

• If there is one address, only lines that match will be processed

Addresses con’t• If two addresses are given, it selects a range

• Once the first address is matched, it and subsequent lines are processed until the second address is matched

• If second address is never matched, processes remainder of lines

• If second addressed matched, sed will then try to match first address again

Instructions• d – does not write out (deletes) selected line

and does not process line any further• n – writes out current line, reads next line,

and processes next program line• a – appends lines after current line• i – inserts lines before current line• c – changes select line so it contains new

text• p – print current line (override –n)

Instructions con’t• w file – write line to a specified file• r file – read contents of file and appends

to current line• q – quits sed immediately

Instructions con’t• s/pattern/replacement-str/[g][p][w file]

– Substitutes first occurrence of pattern with replacement-str

– g replaces all occurences– p prints changed line– w writes changed line to file

• Use & to represent the pattern matched when replacing– Ex. s/a.*/(a.*)/ won’t work … instead use s/a.*/(&)/

Control Structures• ! (NOT) – causes instruction to be performed

on all lines not selected by address(es)• { } (Instruction grouping) – causes multiple

instructions to be run on one address / address pair; separate with semicolons

• : label – identify a location in a sed program• b label – branch to label• t label – conditionally branch to label if last

Substitute instruction was successful

Spaces• sed has two spaces (buffers)

• Think of them like vim’s buffers

• Lines read from input are put in pattern space

• You can also move data back and forth from the hold space (temporary buffer)

Spaces, cont• g – overwrites pattern space with hold space• G – appends hold space to pattern space• h – overwrites hold space with pattern space• H – appends pattern space to hold space• x – swaps the pattern and hold spaces

Examples• sed -n‘/line/ p’ myfile

– Prints out lines in myfile that contain ‘line’

• sed ‘2,4 d’ myfile– Delete lines 2-4, outputs remaining

• sed --in-place ‘2,4 d’ myfile– Deletes lines 2-4 from myfile

• sed ‘s/tea/coffee/g’ myfile– Replaces tea with coffee and prints to screen

More Examples• sed ‘5 q’ myfile

– Prints first five lines then quits ( equiv. head -5)

• sed ‘/^[0-9]/ w newfile’ myfile– Copies lines starting in number to newfile

• sed ‘$ r newfile’ myfile– Appends contents of newfile to end of myfile

• sed ‘G’ myfile– What does this do?

Program File Example1 d

s/company/Company/g

$ a\

Revised 12-1-2005\

by JMH

$ d

Another Program File1 i \

\

Manuf\tModel\tYear\tMiles\tPrice\

=====================================

s/thundbd/tbird/g

s/.*/ &/

$ a \

=====================================