Grep will find a specific string called the pattern from within a target file. The syntax is:
grep [options] [pattern] [target]
Its simplest use is:
grep pattern target
It will return every lines in the target file in which the requested pattern was found.
Useful options
-c- Count and return the number of matches instead of the matches themselves.
-i- Case insensitive.
-m #- Only display the first
# matches.
-v- Inverse match, to find lines which do not contain the pattern.
Using wildcards in Grep
Grep is different from the command line when it comes to wildcards (the * character). In the command line, a * means anything. In grep, * means "any number of the previous thing" (Note: Any number also includes 0), while . means "one instance of anything". The . and * can be combined together to make "any number of anything", the grep equivalent of the command line's *. Below are examples of looking for:
- an "a" followed by a "b" with any one character in between
- an "a" followed by a "b" with any number of spaces in between
- an "a" followed by a "b" with anything in between
grep 'a.b' target
grep 'a *b' target
grep 'a.*b' target
Along with the * for any number of repetitions, you can also use:
- ?
- 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.
- {,m}
- The preceding item is matched at most m times. This is a GNU extension.
- {n,m}
- The preceding item is matched at least n times, but not more than m times.
NOT using wildcards in Grep
Sometimes you actually want to look for a * in a file, which will not really work because it is a wildcard, as mentioned above. In order to do so, you can use '[*]', which will use the literal character * instead of the wildcard. If you are looking for a specific number of multiples, you will need to repeat the whole definition, like '[*][*][*]'. However, if you are looking for any number of *, you can use '[*]*', which is combining the literal character with the "any number of the previous thing" wildcard!
Looking for a set of values
It is also possible to use the square brackets to define a set of values to look for. For either of two values (ex.: 1 or 2), the syntax is '[12]'. When wanting a number or letter in a range, it is possible to use dashes in between the ends of the range. So for example, if looking for a number between 1 and 5, the syntax is '[1-5]'.