Syntax: grep [-option] 'search word' /path/to/file
Options are as follows:
- -i : to ignore word case.
- -r : recursively read files under each directory.
- -w : to get the matching word only.
- -c : to count number of lines that the search word matches.
- -P : to use regex command within the grep command.
- -l : to list the file names matching the search word.
- -v : to get lines that doesn't match the search word.
Sample GREP commands:
- default grep command without using an option.
$ grep 'word' /path/to/file
- ignoring word cases and matches
$ grep -i 'word'/path/to/file
- using regex to get all the lines that matches
$ grep -P "word1|word2" /path/to/file
$ grep -P "^word3" /path/to/file
- matching the exact words in a line
$ grep -w "word" /path/to/file
- get the file names that matches the search word.
$ grep -l "init()" /path/to/file*
- invert matches to the line
$ grep -v "word" /path/to/file
- grep command combining options and uses pipe to execute next grep command.
$ grep -ivP "word1|word2" /path/to/file
$ grep -i "word" /path/to/file | grep -vP "word2"
Nice post. you have indeed covered topic in detail with samples. I have also blogged my experience as grep command in unix with examples
ReplyDelete,let me know how do you find it. Thanks
Some examples on Find Command and Awk Command
ReplyDelete