1 / 19

Chapter 13: sed

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 ]

Télécharger la présentation

Chapter 13: sed

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13:sed Say what?

  2. In this chapter … • Basics • Programs • Addresses • Instructions • Control • Spaces • Examples

  3. sed • GNU sed (stream editor) • Noninteractive, batch editing • Good for repetitive tasks • Often used in a pipe

  4. 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

  5. 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

  6. 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

  7. How sed works • Read one line of input • Read first instruction in program. If the address(es) select this line, runs the instruction on this line • Repeat #2 for each line in the program • Read next line of input and go back to step 2, until there are no more lines of input

  8. 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

  9. 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

  10. 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)

  11. 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

  12. 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.*/(&)/

  13. 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

  14. Spaces • sed has two spaces (buffers) • Think of them like vim’s buffers • Lines read from input are put in patternspace • You can also move data back and forth from the hold space (temporary buffer)

  15. 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

  16. 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

  17. 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?

  18. Program File Example 1 d s/company/Company/g $ a\ Revised 12-1-2005\ by JMH $ d

  19. Another Program File 1 i \ \ Manuf\tModel\tYear\tMiles\tPrice\ ===================================== s/thundbd/tbird/g s/.*/ &/ $ a \ =====================================

More Related