A good source for information on grep is a book called, "The UNIX Programming Environment" by Kernighan & Pike. There's a nice short section on grep (4.1). I have it in my office, if any of my students would like to take a look at it.
The most common use for grep is for finding a particular word or short string in a file. The syntax is:
grep expression filename(s)For example, you might want to find all the lines of code in your C files which refer to a particular variable.
grep 'count' *.cHow can you learn about grep?
1. type (on a Unix machine):man grepYou can copy the manual page for grep to a file using '>' and then print that file. You should learn (or put on your cheat sheet) some of the main options for grep.
2. one of the other TAs has provided some links to documentation with many examples on a FAQ list
3. TRY IT!!
Here's some hints and highlights about grep:
- use single quotes around the expression
- [ ]
- Meaning: one of (in range).
- Examples:
[a3][c5] => a or 3 followed by c or 5, so valid matches are ac,a5,3c,35 [a-z] => any lowercase character between a and z- ^
- Meaning: 'not' if it's inside brackets. Otherwise, it indicates that whatever it precedes must be the first thing on a matching line.
- Examples:
[^ a b c] => matches any lines which have anything other than all a's b's and c's '^a' => matches any lines which start with an 'a'- . (a period)
- Meaning: any single character (like a wildcard)
- Examples:
'^c.b' => matches lines that start with a c and have b as the third character '.' => matches any lines that have any characters- *
- Meaning: zero or more of what precedes it
- Examples:
'^c.*b' => matches lines that start with c and have at least one b 'c.*b' => matches any lines that have a c followed by a b (not necessarily adjacent)- +
- Meaning: one or more of what precedes it
- Examples:
'c+b' => matches lines with cb, ccb, cccb, ccccb, etc. as substrings 'c.+b' => matches lines with cab, caab, cadb, etc. but not cb- ?
- Meaning: zero or one of what precedes it
- Examples:
'c.?b' => matches cb and cab but not caab (and other strings...) '^c- $
- Meaning: whatever precedes it must be the last thing on a matching line
- Examples:
'c.*b$' => matches any lines that have a c and end with a b '^c.*b$' => matches any lines that start with c and end with b- |
- Meaning: or (in egrep)
- Example:
x | y => matches lines with x or y- ()
- Meaning: parentheses are used for grouping
- /
- Meaning: the backslash character is used for escaping characters with reserved meaning (like a period)
- {some number}
- Meaning: repeat the preceding expression a given number of times
- Examples:
c{3} => matches lines with 3 consecutive c's (bc){2} => matches lines with the pattern bcbc- grep -i expression file
- by default, grep is case sensitive. The -i flag makes the match case insensitive
- grep -c expression file
- return only the number of matching lines, instead of printing them
- grep -v expression file
- return all lines in the file not matching the expression