Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sed Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology

Similar presentations


Presentation on theme: "Sed Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"— Presentation transcript:

1 sed Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

2 Dr. Tran, Van Hoai 2007 sed What is " sed "? sed = stream editor  noninteractive, script interpreting  input: file, pipe, keyboard  output: standard output Useful for substitutions on data streams going thru pipelines  " tr " also can be used for substitutions sed can do more

3 Dr. Tran, Van Hoai 2007 sed Typical uses Edit files automatically Simplifying repetitive edits to multiple files Writing conversion programs

4 Dr. Tran, Van Hoai 2007 sed Examples (1) 100xyz1000.0xyz50 400xyz1034xyz20445 300xyz304xyz9344 How to replace "abc" by "\t"? Can we do by " tr "? We can do by "sed" in one command sed 's/abc/\t/g' 100 1000.0 50 400 1034 20445 300 304 9344

5 Dr. Tran, Van Hoai 2007 sed Components s substitute command /../../ delimiter abc search pattern (regex) \t replace pattern (regex)

6 Dr. Tran, Van Hoai 2007 sed How sed works internally? Each line copied into a buffer (pattern space)  sed works on buffer, not the original file All sed commands applied to each line  unless line addressing is specified  subsequent commands operate on current line sed has a hold space for later retrieval

7 Dr. Tran, Van Hoai 2007 sed Syntax Default options sed [-n] [-e] 'command' files -e next argument is an editing command sed [-n] [-f] scriptfile file(s) -f next argument is a script file -n suppress default output Additional options (man pages)

8 Dr. Tran, Van Hoai 2007 sed sed commands General form [address [,address]][!]command [arguments] Pattern addressing ( 0, 1, or more addresses ) AddressMeaning /pattern/ Lines matching pattern \;pattern; Like previous; useful for multiple slashes N Line number N $ The last input line

9 Dr. Tran, Van Hoai 2007 sed How to apply to lines Command specifiedCommand applied to No addressEach input line One addressAny line matching the address Two ","-separated command From first match to second match Address followed by !All lines not matching the address

10 Dr. Tran, Van Hoai 2007 sed Example (2) CommandAction performed s/xx/yy/g Substitute all lines (all occurrences) /abc/d Delete lines containing BSD /^BEGIN/,/^END/ From BEGIN to LINE, inclusive /SAVE/!d Delete lines not containing SAVE /BEGIN/,/END/!s /xx/yy/g ?

11 Dr. Tran, Van Hoai 2007 sed Example (3) Remove "shell comments" Remove "C/C++ comments" Remove assign statements 1000 100 100 # This is a comment 60 60 80 // comment in C++ thisVariable = pow( 10, 100 ); 100.0 60 1000 90 /* This is a comment in C */ Well, this is a sentence in natural language

12 Dr. Tran, Van Hoai 2007 sed Many commands to a match Using braces " { } " Syntax [/pattern/[,/pattern/]]{ command 1 command 2 }

13 Dr. Tran, Van Hoai 2007 sed sed editing ommands d Delete lines s Substitution y Translate characters (like UNIX " tr ")

14 Dr. Tran, Van Hoai 2007 sed sed information commands = Display line number l Display control characters in ASCII p Display a line

15 Dr. Tran, Van Hoai 2007 sed Global replacement By default, UNIX works on line-based principle  read a line, perform an operation on the line By default, sed only work on first occurrence  " s/xx/yy " only replaces the first " xx " to " yy "  " s/xx/yy/g " replaces all " xx "

16 Dr. Tran, Van Hoai 2007 sed Reusing matched string By "&" Change "abc" to "(abc)" sed –e 's/abc/(abc)/g' c.txt sed –e 's/abc/(&)/g' c.txt

17 Dr. Tran, Van Hoai 2007 sed Specifying pattern By " /1 ", " /2 ", etc Keep the first word, delete the second s/\([a-zA-Z]\{1,\}\)* //2

18 Dr. Tran, Van Hoai 2007 sed What is it ? sed 's/[^:]*//2' /etc/password.new

19 Dr. Tran, Van Hoai 2007 sed awk is NEXT


Download ppt "Sed Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"

Similar presentations


Ads by Google