egrep Command Examples in Linux

The egrep command is an offshoot of grep, which allows you to specify POSIX extended regular expressions, which contain more characters for specifying the matching pattern.

egrep searches one or more files for lines that match an extended regular expression regexp. egrep doesn’t support the regular expressions \(,\), \n, \, \{, or \}, but it does support the other expressions, as well as the extended set +, ?, |, and ( ). Remember to enclose these characters in quotes. Exit status is 0 if any lines match, 1 if none match and 2 for errors.

Syntax:

# egrep [options] [regexp] [files]

Search for occurrences of Victor or Victoria in file:

# egrep 'Victor(ia)*' fileegrep '(Victor|Victoria)' file

Find and print strings such as old.doc1 or new.doc2 in files, andinclude their line numbers:

# egrep -n '(old|new)\.doc?' files

egrep Command 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

29. To Print NUM lines of leading context before matching lines:

# egrep -B NUM PATTERN
# egrep --before-context=NUM PATTERN

30. To Print NUM lines of output context:

# egrep -C NUM PATTERN
# egrep --context=NUM PATTERN

31. To Process a binary file as if it were text:

# egrep -a PATTERN /tmp/bin
# egrep -text PATTERN /tmp/bin

32. To assume that the file is of type TYPE:

# egrep --binary-files=TYPE PATTERN

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

# egrep -D ACTION PATTERN
# egrep --devices=ACTION PATTERN

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

# egrep -d ACTION PATTERN
# egrep --directories=ACTION PATTERN

35. To Skip files whose base name matches GLOB:

# egrep --exclude=GLOB PATTERN

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

# egrep --exclude-from=FILE PATTERN

37. To Exclude directories matching the pattern DIR from recursive searches:

# egrep --exclude-dir=DIR PATTERN

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

# egrep -I PATTERN

39. To Search only files whose base name matches GLOB:

# egrep --include=GLOB PATTERN

40. To Read all files under each directory, recursively:

# egrep -r PATTERN
# egrep -R PATTERN

41. To Use line buffering on output:

# egrep --line-buffered PATTERN

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

# egrep --mmap PATTERN

43. To Treat the file(s) as binary:

# egrep -U /tmp/file PATTERN
# egrep --binary /tmp/file PATTERN

44. To Treat the input as a set of lines:

# egrep -z PATTERN
# egrep --null-data PATTERN

45. To display the help:

# egrep -h

46. To print the version number of the grep:

# egrep -V
Related Post