Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.

Similar presentations


Presentation on theme: "Overview of the grep Command Alex Dukhovny CS 265 Spring 2011."— Presentation transcript:

1 Overview of the grep Command Alex Dukhovny CS 265 Spring 2011

2 What is grep Command grep - "general regular expression parser“ Search command for UNIX. Used to search for text strings and regular expressions within one or more files. man grep

3 Common grep Command Options grep [options] pattern [files] -b Display the block number at the beginning of each line. -c Display the number of matched lines. -h Display the matched lines, but do not display the filenames. -i Ignore case sensitivity. -l Display the filenames, but do not display the matched lines. -n Display the matched lines and their line numbers. -s Silent mode. -v Display all lines that do NOT match. -w Match whole word. grep -c Alex my_file.htm

4 How to use grep command Search file for a user – $ grep ad85 /etc/passwd Search file ignoring word case – $ grep -i “ad85" /etc/passwd Search recursively all files and directories under given directory – $ grep -r “ad85" /etc/

5 How to use grep command Search for a specific word in file – $ grep -w “alex" $HOME/cs265.htm Search for 2 different words in file – $ grep -w ‘alex|victoria' $HOME/cs265.htm Count lines that matched in file – $ grep -c 'word' $HOME/cs265.htm

6 How to use grep command Display lines that did not match a pattern – $ grep -v cs265 $HOME/cs265.htm Number of lines that contain matched pattern – $ grep -n 'word' $HOME/cs265.htm Display filenames that matched pattern, but not lines from the files – $ grep -l ‘word' *.htm

7 grep and Wildcards Dot (. ) – matches 1 character Asterisks ( * ) – matches multiple characters Examples: – grep b.g myfile  finds the words “big”, “bag” – grep b*k myfile  finds the word “back”, “buck”, “book”

8 grep and Regular Expressions A "regular expression" is a pattern that describes a set of strings. Regular expressions are used when you want to search for specific lines of text containing a particular pattern.

9 grep and Regular Expressions ^ (Caret) = match expression at the start of a line, as in ^A. $ (Dollar Sign) = match expression at the end of a line, as in A$. \ (Back Slash) = turn off the special meaning of the next character, as in \^. [ ] (Brackets) = 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 grep and Regular Expressions. (Period) = match a single character of any value, except end of line. * (Asterisk) = match zero or more of the preceding character or expression. \{x,y\} = match x to y occurrences of the preceding. \{x\} = match exactly x occurrences of the preceding. \{x,\} = match x or more occurrences of the preceding.

11 grep and Regular Expressions grep bob files {search files for lines with ‘bob'} grep '^bob' files {‘bob' at the start of a line} grep ‘bob$' files {‘bob' at the end of a line} grep '^bob$' files {lines containing only ‘bob'} grep '\^b' files {lines starting with '^b', "\" escapes the ^} grep '[Bb]mug' files {search for ‘Bob' or ‘bob'} grep 'B[oO][bB]' files {search for BOB, Bob, BOb or BoB } grep '^$' files {search for empty lines} grep '[0-9][0-9]' files {search for pairs of numeric digits}

12 grep and Regular Expressions Questions ?


Download ppt "Overview of the grep Command Alex Dukhovny CS 265 Spring 2011."

Similar presentations


Ads by Google