1 / 7

CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed

CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed. Chin-Chih Chang chang@cs.twsu.edu. Regular Expressions. egrep’s extended set includes two special characters - + and ?. They are often used in place of * to restrict the matching scope.

aysel
Download Presentation

CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and 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. CS 497C – Introduction to UNIXLecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Changchang@cs.twsu.edu

  2. Regular Expressions • egrep’s extended set includes two special characters - + and ?. They are often used in place of * to restrict the matching scope. • + - matches one or more occurrences of the previous character. • ? – matches zero or one occurrence of the previous character. $ egrep “true?man” emp.lst

  3. Regular Expressions • The |, ( and ) can be used to search for multiple patterns. $ egrep ‘wood(house|cock)’ emp.lst • sed is a multipurpose too which combines the work of several filters. • Designed by Lee McMahon, it is derived from the ed line editor. • sed is used to perform noniteractive operations.

  4. sed: The Stream Editor • sed has numerous features – almost bordering on a programming language but its functions have been taken over by perl. • Everything in sed is an instruction. An instruction combines an address for selecting lines with an action to be taken on them: sed options ‘address action’ file(s) • The address and action are enclosed within single quotes.

  5. sed: The Stream Editor • The components of a sed instruction are shown as below: sed ’1,$ s/^bold/BOLD/g’ foo address action • You can have multiple instructions in a single sed command, each with its own address and action components. • Addressing in sed is done in two ways: • By line number (like 3,7p). • By specifying a pattern (like /From:/p).

  6. Line Addressing • In the first form, the address specifies either a single line or a set of two (3,7) to select a group of contiguous lines. • The second one uses one or two patterns. • In either case, the action (p, the print command) is appended to this address. • You can simulate head -3 by the 3q instruction in which 3 is the address and q is the quit action.

  7. Line Addressing $ sed ‘3q’ emp.lst • sed uses the p (print) command to print the output. $ sed ‘1,2p’ emp.lst • By default, sed prints all lines on the standard output in addition to the lines affected by the action. So the addressed lines are printed twice.

More Related