Friday 1 July 2016

Linux grep command

How to search string in file in linux?

grep "<search-string>" <file>

Search string by ignoring case with grep.

grep -i "<search-string>" <file>

Search text in one or more files

grep "<search-string>" <file-names-starting>*

Word count with grep?

Example:

grep "2015-05-25 20:19:00" connectProcess.log | grep "successful" | wc -l

Note: Above example give count of lines which has "2015-05-25 20:19:00" and "successful"


How to search string in direcoty using grep?
syntax: grep -nr string my_directory

-n ==> line number in the file
'string' ==> String for search, followed by a wildcard character
-r ==> Recursively search sub directories listed

Exmaple:
grep -nr 'prmconnect' .

here . repesents current directory

No comments:

Post a Comment