egrep command examples

  • About egrep : Print lines matching a pattern
  • It comes from “grep-2.6.3” package.

Examples:

1. To Interpret PATTERN as an extended regular expression

$ egrep –extended-regexp PATTERN
$ egrep -E PATTERN

2. To Interpret PATTERN as a list of fixed strings

$ egrep -F PATTERN
$ egrep –fixed-strings PATTERN 

3. To Interpret PATTERN as a basic regular expression

$ egrep -G PATTERN
$ egrep –basic-regexp PATTERN 

4. To Interpret  PATTERN as a Perl regular expression

$ egrep -P PATTERN
$ egrep –perl-regexp PATTERN 

5. To Use PATTERN as the pattern

$ egrep -e PATTERN
$ egrep –regexp=PATTERN 

6. To Obtain  patterns  from  FILE,  one per line

$ egrep -f FILE, –file=FILE

7. To Ignore case distinctions in both the PATTERN and the input files.

$ egrep -i PATTERN
$ egrep –ignore-case PATTERN 

8. To Invert the sense of matching, to select non-matching lines

$ egrep -v PATTERN
$ egrep –invert-match PATTERN 

9. To Select only those lines containing matches that form whole words.

$ egrep -w PATTERN
$ egrep –word-regexp PATTERN 

10. To Select only those matches that exactly match the whole line.

$ egrep -x PATTERN
$ egrep –line-regexp PATTERN 

11. To ignore the case

$ egrep -y PATTERN

12. To Suppress normal output; instead print a count of matching lines

$ egrep -c PATTERN
$ egrep –count PATTERN 

13. To display in color

$ egrep –color PATTERN

14. To Suppress normal output; instead print the name of each input file, from out will not be expected

$ egrep -L
$ egrep –files-without-match 

15. To Suppress normal output; instead print the name of each input file from which output have been printed

$ egrep -l
$ egrep –files-with-matches

16. To Quiet; do not write anything to standard output Exit immediately with zero status if any match is found

$ egrep -q 
$ egrep –quiet

$ egrep –silent

17. To Stop reading a file after NUM matching lines

$ egrep -m NUM
$ egrep –max-count=NUM 

18. To Print only the matched (non-empty) parts of a matching line

$ egrep -o PATTERN
$ egrep –only-matching PATTERN 

19. To Suppress error messages about nonexistent or unreadable files.

$ egrep -s PATTERN
$ egrep –no-messages PATTERN 

20. To Print  the  0-based  byte  offset  within  the  input  file  before  each  line  of output.

$ egrep -b PATTERN
$ egrep –byte-offset PATTERN 

21. To Print the file name for each match.

$ egrep -H PATTERN
$ egrep –with-filename PATTERN 

22. To Suppress  the  prefixing of file names on output

$ egrep -h PATTERN
$ egrep –no-filename PATTERN 

23. To Display input actually coming from standard input as  input  coming  from  file  LABEL.

$ egrep -cd PATTERN | egrep –label=mysearch -H PATTERN

24. To Prefix each line of output with the 1-based line number within its input file.

$ egrep -n PATTERN
$ egrep –line-number PATTERN 

25. To Make  sure  that  the  first  character  of  actual  line  content lies on a tab stop

$ egrep -T PATTERN
$ egrep –initial-tab PATTERN

26. To Report  Unix-style  byte  offsets

$ egrep -u PATTERN
$ egrep –unix-byte-offsets PATTERN 

27. To Output a zero byte instead of the character that normally follows a file name.

$ egrep -Z PATTERN
$ egrep –null PATTERN
 

28. To Print  NUM  lines  of  trailing  context  after  matching lines

$ egrep -A NUM PATTERN

$ egrep –after-context=NUM PATTERN 

To Print  NUM  lines  of  leading  context  before  matching lines

$ egrep -B NUM PATTERN

$ egrep –before-context=NUM PATTERN 

To Print  NUM  lines  of  output  context

$ egrep -C NUM PATTERN

$ egrep –context=NUM PATTERN 

To Process a binary file as if it were text

$ egrep -a PATTERN /tmp/bin

$ egrep -text PATTERN /tmp/bin 

To assume that the file is of type TYPE.

$ egrep –binary-files=TYPE PATTERN 

To If  an  input  file  is a device, FIFO or socket, use ACTION to process it

$ egrep -D ACTION PATTERN

$ egrep –devices=ACTION PATTERN 

To If  an  input  file  is  a directory, use ACTION to process it

$ egrep -d ACTION PATTERN

$ egrep –directories=ACTION PATTERN 

To Skip files whose base name matches GLOB

$ egrep –exclude=GLOB PATTERN 

To Skip  files  whose  base  name  matches  any of the file-name globs read from FILE

$ egrep –exclude-from=FILE PATTERN 

To Exclude directories matching the pattern DIR from recursive searches

$ egrep –exclude-dir=DIR PATTERN 

To Process a binary file as if  it  did  not  contain  matching  data

$ egrep -I PATTERN 

To Search  only  files  whose  base  name  matches  GLOB

$ egrep –include=GLOB PATTERN 

To Read all files under each directory, recursively

$ egrep -r PATTERN

$ egrep -R PATTERN 

To Use line buffering on output

$ egrep –line-buffered PATTERN 

To If possible, use the mmap system call to read input, instead  of  the  default  read

$ egrep –mmap PATTERN 

To Treat the file(s) as binary

$ egrep -U /tmp/file PATTERN

$ egrep –binary /tmp/file PATTERN 

To Treat the input as a set of lines

$ egrep -z PATTERN

$ egrep –null-data PATTERN 

To display the help

$ egrep -h 

To print the version number of the grep

$ egrep -V 

Leave a Reply

Your email address will not be published. Required fields are marked *