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

Overview of the grep Command

  • Upload
    nuwa

  • View
    36

  • Download
    4

Embed Size (px)

DESCRIPTION

Overview of the grep Command. Alex Dukhovny CS 265 Spring 2011. 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. Common grep Command Options. - PowerPoint PPT Presentation

Citation preview

  • Overview of the grep CommandAlex DukhovnyCS 265Spring 2011

  • What is grep Commandgrep - "general regular expression parserSearch command for UNIX.Used to search for text strings and regular expressions within one or more files.man grep

  • Common grep Command Optionsgrep [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

  • How to use grep commandSearch 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/

  • How to use grep commandSearch for a specific word in file$ grep -w alex" $HOME/cs265.htmSearch for 2 different words in file$ grep -w alex|victoria' $HOME/cs265.htmCount lines that matched in file$ grep -c 'word' $HOME/cs265.htm

  • How to use grep commandDisplay lines that did not match a pattern$ grep -v cs265 $HOME/cs265.htmNumber of lines that contain matched pattern$ grep -n 'word' $HOME/cs265.htmDisplay filenames that matched pattern, but not lines from the files$ grep -l word' *.htm

  • grep and WildcardsDot ( . ) matches 1 characterAsterisks ( * ) matches multiple charactersExamples:grep b.g myfile finds the words big, bag

    grep b*k myfile finds the word back, buck, book

  • grep and Regular ExpressionsA "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.

  • 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].

  • 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.

  • grep and Regular Expressionsgrep 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}

  • grep and Regular Expressions

    Questions ?