siwen

siwen

😉

grep command

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#

OptionFunction
-cCount the number of matching lines in a file
-iIgnore case in the pattern
-nList all matching lines and display line numbers
-vList lines that do not match the pattern
-eLogical OR between multiple options
-oOnly display the content matched by the pattern
-lList the filenames with matching content
-wMatch whole words
-qQuiet mode, do not output any information
-rRecursively search
-fMatch multiple patterns based on file content
-AAfter, display n lines after the matched line
-BBefore, display n lines before the matched line
-CContext, display n lines before and after the matched line
-EUse extended regular expressions, equivalent to egrep
-FEquivalent to fgrep, does not support regular expressions
-PUse Perl regular expressions

Use the help command to view all options (grep --help or man grep)

  • grep uses "standard regular expressions" as the matching standard
  • egrep is an extended version of the grep command, equivalent to grep -E, which uses extended regular expressions as the matching standard
  • fgrep 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
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.