Wednesday, 1 July 2015

Grep command options

grep prints lines of input matching a specified pattern.

1. Search for the given string in a single file
Syntax:
grep "literal_string" filename

2. Checking for the given string in multiple files.
e.g.
$ cp demo_file demo_file1
$ grep "this" demo_*

3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE

4: -c, --count
 Instead of the normal output, print a count of matching lines for each input file.

5: -v , --invert-match
With the -v, --invert-match option, count non-matching lines.

6: -w, --word-regexp
Select only those lines containing matches that form whole words.

7: -x, --line-regexp
Select only those matches that exactly match the whole line.

8: -L, --files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
The scanning of each file stops on the first match.

9: -l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed.
The scanning of each file stops on the first match.

10: -m num, --max-count=num
Stop reading a file after num matching lines.

11: -o, --only-matching
Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line.

12: -q, --quiet, --silent
Quiet; do not write anything to standard output.
Exit immediately with zero status if any match is found, even if an error was detected.

13: -s, --no-messages
Suppress error messages about nonexistent or unreadable files.

14: Context Line Control


  • -A NUM, --after-context=NUM 
    • Print NUM lines of trailing context after matching lines. 
    • Places a line containing a group separator (--) between contiguous groups of matches. 
    • With the -o or --only-matching option, this has no effect and a warning is given.
  • -B NUM, --before-context=NUM
    • Print NUM lines of leading context before matching lines. 
    • Places a line containing a group separator (--) between contiguous groups of matches. 
    • With the -o or --only-matching option, this has no effect and a warning is given.

No comments:

Post a Comment