Monday, 14 March 2016

Searching file content in Linux/Unix

1. Check Grep version
grep -V

2. Search single word in single file / multifles
grep <string> *

3. Search Multiple words in single file / multiple files
grep -E 'word1|word2'

4. Seach text which is not maching to string
grep -v "String" fileName

5. Print the matching string and its before lines
grep ens33 -A 4

6. Print the matching string and its after lines and its around lines
grep enss33 -B 4
grep ens33 -C 4


7. Search a string Recursively
grep -r <string> <fileName>

8. Grep the string with highlighted in color
grep --color=auto <string> <file Name>
export GREP_COLOR='1;30;43'


9. display the lines which does not matches all the given strings
grep -v -e "a" -e "b" -e "c" filename.txt

10. Get the count of given string from file
grep -c <string> filename

11. Search for files which are matching to the given string
grep -l this demo*

12. Beginning of line (^) using cap symble
grep "^Nov 10" messages.1

13. End of the line ( $) using dollar symble
grep "terminating.$" messages



? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.

Few examples about grep command..

Comment one grep command below in comment section....

No comments:

Post a Comment