Commonly Used Regular Expressions
Search for a specific string
Use dmesg to list the core information and then use grep to find out which line contains "tap" , and to color the captured keywords, and add the line number to indicate:
dmesg -a | grep -n --color=auto 'tap'
The first two lines and the last three lines of the line where the keyword is located are also captured and displayed:
dmesg -a | grep -n -A3 -B2 --color=auto 'tap'
dmesg is a command on most Unix-like operating systems that prints the message buffer of the kernel. The output includes messages produced by the device drivers.
Suppose we want to get the specific string "abc" from the file just now, the easiest way is like this:
grep -n 'abc' regular_express.txt
- -n Show line number.
I want to display the line where the string abc is a specific ignoring case:
grep -in 'abc' regular_express.txt
- -i Ignore case.
What we want is that the line does not have the string "abc" to be displayed on the screen:
grep -vn 'abc' regular_express.txt
- -v Reverse selection.
Use square brackets [] to search for collective characters.
The string I need is just "tast" or "test".
grep -n 't[ae]st' --color=auto regular_express.txt
I don't want the line with "g" before "oo".
grep -n '[^g]oo' --color=auto regular_express.txt
I don't want lowercase characters in front of "oo".
grep -n '[^a-z]oo' --color=auto regular_express.txt
or
grep -n '[^[:lower:]]oo' --color=auto regular_express.txt
We want to get the row with numbers.
grep -n '[0-9]' --color=auto regular_express.txt
or
grep -n '[[:digit:]]' --color=auto regular_express.txt
Start and end of line characters ^ and $.
I want "the" to be listed only at the beginning of the line:
grep -n '^the' --color=auto regular_express.txt
I want to list the line that starts with a lowercase character:
grep -n '^[a-z]' --color=auto regular_express.txt
or
grep -n '^[[:lower:]]' --color=auto regular_express.txt
I want to list the lines that do not start with English letters:
grep -n '^[^a-zA-Z]' --color=auto regular_express.txt
- ^ in [] represents reverse selection
- ^ outside of [] represents the meaning of positioning at the beginning of the line.
grep -n '^[^[:alpha:]]' --color=auto regular_express.txt
I want to find out the line that ends with a decimal point (.):
grep -n '\.$' --color=auto regular_express.txt
- Because the decimal point(.) has other meanings, the escape character (\) must be used to remove its special meaning!
I want to find out which line is a "blank line", that is, there is no data entered in this line:
grep -n '^$' --color=auto regular_express.txt
When we want to see the configuration file of a certain software, we don't want to see blank lines and lines that have been commented out(beginning with #).
grep -v '^$' /etc/syslog.conf | grep -v '^#'
Any one character (.) And repeated characters (*).
- . (Decimal point): means "There MUST be an arbitrary character".
- * (Asterisk): means "repeat the previous character, 0 to infinite times", which is a combination form.
grep -n 'g..d' --color=auto regular_express.txt
When we need to search contain at least two "o" or more substrings, we need "ooo*", which is:
grep -n 'ooo*' --color=auto regular_express.txt
If I want to find the string beginning with g and ending with g, the characters in it are optional:
grep -n 'g.*g' --color=auto regular_express.txt
If I want to find the rows that contain any number:
grep -n '[0-9][0-9]*' --color=auto regular_express.txt
or
grep -n '[0-9]' --color=auto regular_express.txt
Limit the range of consecutive RE characters {}
Because the symbols of { and } have special meanings in the shell, we must use the escape character \ to make them lose their special meaning.
Suppose I want to find two strings of "o":
grep -n 'o\{2\}' --color=auto regular_express.txt
What if I want "goooo....g" with more than 2 "o":
grep -n 'go\{2,\}g' --color=auto regular_express.txt
or
grep -n 'gooo*g' --color=auto regular_express.txt
Grep "non-greedy" matching with -P option (only on Linux). Match the comment of test.html.
grep -P '<!--.*?-->' test.html --color=auto
And Greedy match is default as following:
grep '<!--.*-->' test.html --color=auto
Use ( and ) to match multiple characters
Match the comments ( /*...*/ ) of C language.
grep -rn '\(/\*\).*\(\*/\)' test.txt --color=auto
Comments
Post a Comment