grep
grep is used for finding strings within files or groups of files. A simple example of looking for the string "foo" in the file myfile.txt is
grep foo myfile.txt
. Grep has many modification flags that can be used to narrow the searching process or change the output process. In general, the grep command format is
grep [modifier flags] <string to find> <search path>
A few useful examples:
- find how many lines foo appears on in myfile.txt
grep -c foo myfile.txt
- find Foo or fOO or FoO in myfile.txt
grep -i foo myfile.txt
- list file names that have foo in the file within the current directory
grep -l foo *
- show 3 lines before and 2 lines after the line with foo
grep -B3 -A2 foo myfile.txt
- show lines that don't have foo
grep -v foo myfile.txt
- find lines that begin with foo
grep "^foo" myfile.txt
- find lines that end with foo
grep "foo$" myfile.txt
- find file name with foo in all sub-directories
grep -l -R foo *
- show the found string from a regular expression search
- grep has powerful regular expression capability. This allows searching for variable strings. A single "." will match any single character (any printable character).
grep -o "^[[:alpha:]]\{3,6\}" myfile.txt
- search for lines that begin with foo and end with bar and have any alphanumeric stuff between
- This will find
grep "^foo[[:alnum:]]bar$" myfile.txt
but it will not findfoo3bar fooabar
because the space is not an alphanumeric character. Other predefined character sets are [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:] (xdigit is hexadacimal digit 0-9A-F). These are called "bracket expressions". grep will match a single character in the set. To match more than one you append a counting modifier to the regular expression like:foo bar
? The preceding item is optional and matched at most once. * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. {n} The preceding item is matched exactly n times. {n,} The preceding item is matched n or more times. {n,m} The preceding item is matched at least n times, but not more than m times.
- To find foo followed by 1 or more alphabetic characters
grep "foo[[:alpha:]]\+" myfile.txt
Here is a text file to run some practices on using grep. Copy and save to a file called test.txt and try the following:The "+" has to be escaped in the double quotes due to shell expansion issues as does the ?, {, } and . characters"
- How many lines have the string "sed"?
- How many lines have the string "Sed" at the beginning of the line?
- How many blank lines are there?
- What do you find searching for the string "Pro" or "pro" followed by 1 to 3 letters or digits, a single space, a word of unknown length, another space followed by an unknown number of digits?
- How many periods are at the end of a line?
- How many periods are there?
Answers below
grep answers
grep |
---|
|
Attachments:
testtext (text/plain)