3 minutes to finish the regular expression of Linux system
A regular expression is a pattern of characters used to match specified characters during a lookup. Metacharacters are generally divided into two categories in Linux: Shell metacharacters, which are parsed by the Linux Shell;
Regular expression metacharacters, parsed by text processing tools such as vi/grep/sed/awk;
Regular expressions are typically processed as lines of text. Set the--color parameter for the grep command before doing the following example:
class="">$ alias grep='grep --color=auto'
Each time the string is filtered out, it will be colored.
One more thing you need to do before you start is create a re-file for testing, which reads as follows:
class="">$ cat re-fileI had a lovely time on our little picnic.Lovers were all around us. It is springtime. Ohlove, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.is forever. I live for you. It's hard to get back in thegroove. class="">$ cat linux.txtLinux is a good god assdxw bcvnbvbjkgreatttttt wexcvxcoperaaaating dhfghfvxgooodfs awrerdxxhklgdsystem awxxxxgladgood
regular expression metacharacters
special metacharacters
Extended regular expressions
practical operation
Matches all rows that begin with love
class="">$ grep '^love' re-filelove, how much I adore you. Do you know
Match all lines at the end of love
class="">$ grep 'love$' re-fileclover. Did you see them? I can only hope love.
Matches all lines that start with l, contain two characters in the middle, and end with e
class="">$ grep 'l.. e' re-fileI had a lovely time on our little picnic.love, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.is forever. I live for you. It's hard to get back in the
Matches 0 or more blank lines followed by love characters
class="">$ grep ' *love' re-fileI had a lovely time on our little picnic.love, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.
Match Love or Love
class="">$ grep '[Ll]ove' re-file #case insensitive to l I had a lovely time on our little pic.Lovers were all around us. It is springtime. Ohlove, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.
Match letters A-Z, followed by ove
class="">$ grep '[A-Z]ove' re-fileLovers were all around us. It is springtime. Oh
Matches any character line that is not in the A-Z range, all lowercase characters
class="">$ grep '[^A-Z]' re-fileI had a lovely time on our little picnic.Lovers were all around us. It is springtime. Ohlove, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.is forever. I live for you. It's hard to get back in thegroove.
matching love
class="">$ grep 'love\. ' re-fileclover. Did you see them? I can only hope love.
matching space
class="">$ grep '^$' re-file
Match any character
class="">$ grep '.* ' re-fileI had a lovely time on our little picnic.Lovers were all around us. It is springtime. Ohlove, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.is forever. I live for you. It's hard to get back in thegroove.
Repeat the preceding o character 2 to 4 times
class="">$ grep 'o\{2,4\}' re-filegroove.
Repeat the o character at least 2 times
class="">$ grep 'o\{2,\}' re-filegroove.
Repeat 0 character up to 2 times
class="">$ grep 'o\{,2\}' re-fileI had a lovely time on our little picnic.Lovers were all around us. It is springtime. Ohlove, how much I adore you. Do you knowthe extent of my love? Oh, by the way, I thinkI lost my gloves somewhere out in that field ofclover. Did you see them? I can only hope love.is forever. I live for you. It's hard to get back in thegroove.
Repeat the previous character one or more times
class="">$ egrep "go+d" linux.txtLinux is a goodgod assdxw bcvnbjkgoodfs awrerdxxhklgood###0 or one character ansheng@Ubuntu:/tmp$ egrep "go? d" linux.txtgod assdxw bcvnbvbjkgdsystem awxxxx
or, find multiple strings
class="">$ egrep "gd|good" linux.txtLinux is a goodgdsystem awxxxxgood
Group filtering matching
class="">$ egrep "g(la|oo)d" linux.txtLinux is a goodgladgood