grep command can be used to search the named input FILEs, or standard input if no files are named, for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
Suppose you have these files:
file.txt | file2.txt |
word1 word2 word3 hello world! word5 word6 | word1 word2 hello friend! word4 word5 word6 |
- Print the lines containing "hello" from file.txt:
- Search in multiple files:
$ grep 'hello' file.txt file2.txt
$ grep 'hello world' file.txt
$ cat file.txt | grep 'hello'
$ grep -i 'hello' file.txt
- Search recursively for a string inside files in directory:
$ grep -r "hello" /home/abc
/home/file.txt:hello world!
- Same result, without showing file name:
$ grep -h -R "hello" /home/abc
- Print the number of lines in file, containing the string:
$ grep -c 'hello' file.txt
- Print also the line number in file, where the string is found:
$ grep -n 'hello' file.txt
- Invert match: print only those lines that do not contain the given word:
$ grep -v 'hello' file.txt
- List all the files with .txt extension containing the string:
- Search exactly for a string inside a file:
$ grep -x 'hello world' file.txt
$ grep -x 'hello world!' file.txt
- Use colors to highlight matches:
$ grep --color hello file.txt
No comments:
Post a Comment