Learn GNU/Linux Commands (15): Search Content - grep
The program "grep" search the content of files. But default, it gets the input from standard input. But it can also read the content of files. With piping, "grep" is often used to search other programs' output.
grep
grep PATTERN [FILE]...
Search for the regular expression PATTERN in each FILE and print the matching lines by default. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.
To end the search in standard input, press CTRL+D for EOF (end of file) or CTRL+C.
By default, "grep" is case sensitive.
"grep" options:
- -i, --ignore-case: case insensitive.
- -v, --invert-match: select lines that don't match.
- -r, --recursive: read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory.
Search for Multiple Patterns
To match lines that match multiple patterns simultaneously, use "grep" again with piping.
grep PATTERN1 [FILE]... | grep PATTERN2 | grep PATTERN3 ...
Comments
Post a Comment