Wednesday, September 25, 2013

GREP command with examples

GREP command is used to search a pattern specified by user in the given text or files or block passed from first command to it through the pipe symbol (symbolic representation |)
Syntax:
$ grep [options] pattern [files]
The complete list of options can be accessed with grep --help command. If you want to match more than one word pass  within double quotes” “
$ grep "Google is search engine" test1 test2 test3
It will list all lines of these three file containing “Google is search engine” in it in separate line, each line will be preceded with name of the file in which they appear. If you do not want the file name you can use -h option as below
$ grep -h "Google is search engine" test1 test2 test3
It will not precede the matched line with the file in which they are found
$ grep -i "Google is search engine" test1 test2 test3
The above command with option -i will tell to ignore the case i,e “google is SEARCH engine” will also match
$ grep "Google is search engine" *
This will search the text “Google is search engine” in all the text file in the current directory
$ grep -r "Google is search engine" *
The option -r will make it to search the text “Google is search engine” in all the text file recursively in the current folder as well as all in child folder.
$ grep -c "Google is search engine" test1 test2 test3
It will not print each line but just return the count of match in each file
$ grep -n "Google is search engine" test1 test2 test3
It will also print the line number of the line containing the pattern in each file
$ grep -rni "Google is search engine" *
The above command will recursively search each directory, print the line no and will ignore the case.




Also you can refer to:

Touch command with examples
RSYNC Command without Authentication

No comments:

Post a Comment