“grep” stands for “Global Regular Expressions Print”. The grep filter scans a file for a certain sequence of characters and shows all lines containing that sequence. Regular expression refers to the pattern that is searched inside the file.
Command
$ grep [options] pattern [file] |
Options
Here are details about some options.
Option | Details |
---|---|
-i | Ignore case differences in patterns and input data so that characters that only differ in case will match. |
-f | Obtain patterns from File, one per line. |
-e | This option allows you to provide an expression. Can use numerous times. |
-h | Display the matched lines, but do not display the filenames. |
-c | Print out the number of matching lines in each file. |
-l | Only shows a list of filenames. |
-n | Show the lines that match and their line numbers. |
-v | This makes all the lines that do not fit the pattern print out. |
-w | Select only those lines containing matches that form whole words. |
-o | Only print the parts of a line that match, with each part on a separate line. |
-R | Read all files under each directory, recursively. |
Examples
- Now, let us see some examples.
grep -i linux linux_introduction.txt
grep -c Linux linux_introduction.txt
grep -w Linux linux_introduction.txt
grep -n Linux linux_introduction.txt
grep -l Linux *
grep -iR Ubuntu .