grep Text Filtering Tool#
Description:
grep
(global search regular expression (RE) and print out the line) is a powerful text search tool that can search text using regular expressions and print out the matching lines. It is used to filter specific characters in a search. It can be used with various commands using regular expressions, making it very flexible to use.
Syntax:
grep [OPTION...] PATTERNS [FILE...]
grep [OPTION...] -e PATTERNS ... [FILE...]
grep [OPTION...] -f PATTERN_FILE ... [FILE...]
grep Command Options#
Option | Function |
---|---|
-c | Count the number of matching lines in a file |
-i | Ignore case in the pattern |
-n | List all matching lines and display line numbers |
-v | List lines that do not match the pattern |
-e | Logical OR between multiple options |
-o | Only display the content matched by the pattern |
-l | List the filenames with matching content |
-w | Match whole words |
-q | Quiet mode, do not output any information |
-r | Recursively search |
-f | Match multiple patterns based on file content |
-A | After, display n lines after the matched line |
-B | Before, display n lines before the matched line |
-C | Context, display n lines before and after the matched line |
-E | Use extended regular expressions, equivalent to egrep |
-F | Equivalent to fgrep, does not support regular expressions |
-P | Use Perl regular expressions |
Use the help command to view all options (grep --help or man grep)
grep
uses "standard regular expressions" as the matching standardegrep
is an extended version of the grep command, equivalent togrep -E
, which uses extended regular expressions as the matching standardfgrep
is a simplified version of the grep command, does not support regular expressions, but has fast search speed and low system resource usage
Examples#
# Count the number of empty lines
[root@localhost ~]# grep -c '^$' test.txt
# Lines containing the character 'h' (case insensitive)
[root@localhost ~]# grep -i 'h' test.txt
# List all empty lines and display line numbers
[root@localhost ~]# grep -n '^$' test.txt
# List all non-empty lines
[root@localhost ~]# grep -v '^$' test.txt
# List all lines starting with 'S' and empty lines
[root@localhost ~]# grep -e '^$' -e '^S' test.txt
# Filter all numbers and only display the content matched
[root@localhost ~]# grep -o '[0-9]' test.txt
# List the filenames that contain the content 'a'
[root@localhost ~]# grep -l 'a' demo.txt test.txt pwd.txt
# Match lines that contain the word 'you'
[root@localhost ~]# grep -w 'you' test.txt
# Match based on the patterns in the file
[root@localhost ~]# grep -f file test.txt
# List lines containing 'can' and the following three lines
[root@localhost ~]# grep -A3 'can' test.txt
# List lines containing 'can' and the previous three lines
[root@localhost ~]# grep -B3 'can' test.txt
# List lines containing 'can' and the previous and following three lines
[root@localhost ~]# grep -C3 'can' test.txt
# Match the character 'd' at least 2 times
[root@localhost ~]# grep -E 'd{2,}' test.txt