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.
or
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.
Suppose I need to find the string of "g??d", that is, there are four characters in total, starting with "g" and ending with "d", I can do this:
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

Popular posts from this blog

BdsDex: failed to load Boot0001 "UEFI BHYVE SATA DISK BHYVE-OABE-20A5-E582" from PciRoot(0x0)/Pci (0x2, 0x0)/Stat(0x0,0xFFFF,0x0) : Not Found

How To Install Nginx, MySQL and PHP (FEMP) Stack on FreeBSD 13.0

Install samba on FreeBSD(on VMware Workstation) to share files with Window.