1 / 12

An Overview of the grep Command

An Overview of the grep Command. grep Regular Expressions and their Usage Kristian Calhoun CS265-001 Prof. Nowak 29 September 2010. What is grep ?. % man grep DESCRIPTION

petra-frost
Download Presentation

An Overview of the grep Command

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. An Overview of the grepCommand grepRegular Expressions and their Usage Kristian Calhoun CS265-001 Prof. Nowak 29 September 2010

  2. What is grep? • % man grep • DESCRIPTION • The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses a compact non-deterministic algorithm. • Can search either a specified file or standard input • Be careful: certain characters such as $, *, [, ^, |, (, ), and \ are also meaningful to the shell.

  3. grep options pattern input_file_names • Other variants of grep • fgrep – “fixed grep” (also known as “fast grep”) –does not support regular expressions (uses fixed strings). • egrep – supports the extended set +, ?, | , and ( ) – supports some metacharacters, but not others such as \(, \) , \n, \<, \> A metacharacter is a character that has a special meaning to a computer program, such as a shell interpreter or a regular expression engine. It is therefore safest to enclose the pattern list in single quotes (‘... ‘) to avoid confusion.

  4. Useful grep options Many more advanced options are available in UNIX Resource #3 on course website or by checking man grep. -ior--ignore-case -v or--invert-match //Selects non-matching lines -w //Select only those lines containing matches that form whole words. -c //Prints only a count of the lines that contain the pattern. -n //Precede each line by its line number in the file (first line is 1).

  5. Examples The file users contains 18 lines. Shown are examples of the results from combining multiple grep options.

  6. Examples (Cont’d) • % grep -i -w main * • search files in the current working directory for the word main, any case • % who | grep kc498 • send the output of who to grep and look for matches of kc498

  7. Searching for text patterns with regular expressions A regular expression describes a pattern in a set of strings. As in the previous examples, letters and numbers are regular expressions of themselves. Use the escape character \ to precede any special meta-character. (Also has other uses when used with different characters.) Regular expressions can be concatenated Use parentheses to override precedence and group.

  8. Repetition and Placement Operators

  9. Searching for ranges and multiple matches • {n} • Match preceding item exactly n times • {n,} • Match preceding item at least n times • {,m} • Match preceding item at mostm times • {n,m} • Match preceding item between n and m times. • [ ] • match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9]. • [^ ] • match any one character except those enclosed in [ ], as in [^0-9]

  10. More special uses of \ • \sMatches any single whitespace character (space, tab, etc.) • \S Matches any single non-whitespace character • \W Matches any non-alphanumeric character • \r carriage return • \n line feed \b Matches a word boundary. Ex: \dog\b will match occurrences of the word “dog” (similar to –w). \d Matches any digit, the same as [0-9] \D Matches any non-digit, the same as [^0-9] \w Wildcard—Matches any alphanumeric character, including underscore. Equivalent to [A-Za-z0-9_]

  11. Regular Expression Tester http://www.cs.drexel.edu/~introcs/Fa09/extras/RegExp/RegExp_tester.html

  12. References Robbins, Arnold. UNIX In a Nutshell. 4th ed. Safari Books Online: O'Reilly, 2005. Web. Magloire, Alain, et al. GNU Grep: Print lines matching a pattern. Free Software Foundation, Inc. 8 September 2010. Web. Wikipedia Contributors. "Metacharacter." Wikipedia. 17 May 2010. Web. 24 Sept. 2010. <http://en.wikipedia.org/wiki/Metacharacter>. "Regular Expressions in Grep." Robelle: Solid Software for HP Servers. Web. 24 Sept. 2010. <http://www.robelle.com/smugbook/regexpr.html>. "About Regular Expressions." Drexel University Computer Science. Web. 25 Sept. 2010. <http://www.cs.drexel.edu/~introcs/Fa09/extras/RegExp/regular_expr.html>.

More Related