Category: Entertainment

Presentation Description

No description available.


Presentation Transcript

Introduction to Sed:

Introduction to Sed 1

Sed – stream editor:

2 Sed – stream editor Supports many types of editing operations, we look at one very useful one – substitution. s/regexp/replacement/modifier sed 's/[aeiou]/\$/g'

sed Command Line:

3 sed Command Line -n means supress default output (doesn’t print anything unless you tell it to). -f commands are in the file scriptfile -e used when specifying multiple commands from the command line.

Some Commands:

4 Some Commands d delete line p print line

Some examples:

5 Some examples /Windows/d deletes lines that contain the word Windows. /[Uu]nix/!d deletes lines that do not contain the word unix. /[0-9]/p prints lines that contain any digits.

Entire command lines:

6 Entire command lines > sed ‘1,5/d’ somefile > sed –n ‘/foo/p’ /usr/dict/words > sed –n ‘/START/,/STOP/p’ prints everything but the first 5 lines of the file somefile (same as tail +6 somefile ). prints everything from STDIN between a line that contains START and one the contains STOP just like grep foo /usr/dict/words


7 substitutions s/regexp/replacement/[flags] replaces anything that matches the regular expression with the replacement text. sed –n ‘s/[0-9]/#/’ matches every line (no addresses!) replaces the first digit on line with “#”

Substitution Flags:

8 Substitution Flags g global (replace all matches on the line). p print the line if a successful match w write the line to a file if a match was found. n replace the nth instance (match) only.

substitution examples:

9 substitution examples sed ‘s/[wW]indows/Unix/g’ sed –n ‘s/f/F/gp’ print every line containing ‘f’ after replacing all ‘f’s by ‘F’s.

sed scripts:

10 sed scripts Series of commands can be put in a file and use the ‘-f’ option. Can also create an sed script: #!/bin/sed –nf s/vi/emacs/g /[Ww]indows/d p

Possibly Interesting Example:

11 Possibly Interesting Example sed script to remove all HTML tags from a file: #!/bin/sed –nf # sed script to remove tags s/<[^>]*>//g p

The End:

The End 12