Week 11 Lecture.docx - Week 11 Lecture – awk and sed sed Stream editor Syntax sed[address1,address2 command[args file command and file must be

Week 11 Lecture.docx - Week 11 Lecture – awk and sed sed...

This preview shows page 1 - 4 out of 9 pages.

Week 11 Lecture – awk and sed sed Stream editor Syntax: sed [address1][,address2][!] command [args] file command and file must be specified For sed, looking at a file (the file indicated), and want to edit that file without using vi or any kind of editor. Want to edit it just by using a command. Eg: sed 2,7 filename doing something from lines 2 through 7. sed 2 filename modifying only address 2 sed 2,99 filename modify lines 2 to 99 ! reverses the command; does the complement of what the command does. Optional Commands in sed are single letters ; a = add a line/append after; i = insert a line, d = delete sed 2,99 d file address 1 is line 2, address 2 is ,99, and d means delete lines 2 to 99. Will see file, but no lines 2-99. sed 2,99 !d file delete everything except lines 2 to 99. Deletes line 1, 100, etc. Some commands don’t take any arguments. Eg d command does not take any arguments. sed ‘d’ file means all addresses sed ‘1d’ file deletes line 1 of file sed ‘2,5d’ lines deletes lines 2-5 sed ‘2,5!d’ lines delete everything but lines 2-5. Always put single quotations to prevent bash substitution in awk, sed and grep commands!! Re in sed are delimited by forward slashes. The regular expression is address 1. o sed ‘/^#/d’ deleting all lines that begin with a hashtag. Address 1 and address 2 don’t have to be numbers; they can be regular expressions sed ‘11,$d’ delete from lines 11 to the end of the file; essentially left with the first 10 lines. sed ‘1,4d 6,9d’ after first d, entered new line character. Here, means want to delete lines 1-4 and 6-9. When enter key is hit before the 2 nd quotation, get a secondary prompt because haven’t finished typing in. sed -e ‘1,4d’-e ‘6,9d’ instead of having it on 2 separate lines, can use -e. 1 st expression and 2 nd expression are separated by -e sed ‘/debug/d’ wherever you see a line with debug in it, delete it. sed ‘/i/d’ delete any line with an i in it; i is the regular expression sed ‘3 a\Hello World’ address 1 is line 3, and the command is a (add after). So means add Hello World after line #3. Eg One Two
Image of page 1
Three Hello World Four sed ‘/john/ a\ My name is’ when see john, append My name is sed ‘/two/ a\ lines one two My name is Three four sed ‘10c\ new contents for line 10’ change line 10 to “new contents for line 10” sed ‘s/foo/bar’ s=substitute. Whenever see foo, substitute with bar sed ‘s/five/5’ file one two three four 5 six sed ‘s/e/E’ file changes only the first E in each line onE two thrEe four fivE six sEven Eight ninE tEn sed ‘s/e/E/g’ lines changes all E’s in each lines. No addresses here; changes the whole file. s is the command (substitute); has an argument of ‘g’ which means global. g is the argument to the substitute command want to change all the e’s on the command, not just the first e.
Image of page 2
sed ‘1,2s/line/LINE/’
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 9 pages?

  • Fall '10
  • michaelhedenreich

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors