Shell programming three swordsman grep and regular expressions
A grep tool
grep is a row filtering tool, used to filter rows based on keywords
Syntax:
grep-option "keyword" file name
1.1 Common options
Options | meaning |
---|---|
-i | not case sensitive |
-v | Find rows that do not include keywords, that is, reverse selection |
-w | Search by word, match some and only characters |
-The | Only display the matched string |
-c | Count the number of matches |
-n | Show line number |
-r | Traverse the directory layer by layer to find |
-A n | Display matching lines and the following (after) n lines |
-B n | Show matching lines and n lines before (before) |
-C n | Display matching lines and n lines before and after |
-e | Use regular expressions |
-E | Use extended regular expressions |
-P | Match numbers, letters or strings |
1.2 Option explanation
The test text content is as follows:
grep -w
command: grep -w'cen' sedtest.txt
can be seen from the figure, when the option -w is not included, as long as the text contains "cen", it will be filtered out, and after -w is required Only if the word exists in the text will be filtered
grep -o
命令 : grep -o 'cenjeal' sedtest.txt
As can be seen from the figure, when -o is not included, a whole line will be output, and after the -o option is used, only the matching string will be displayed
grep -P
command: grep -P'\d' sedtest.txt matches numbers
Command: grep -P'\w' sedtest.txt matches alphanumeric underscores
Command: grep -P'\s' sedtest.txt matches strings
Two regular expressions
2.1 The meaning of regular expressions
Regular expression (Regular Expression, regex or regexp, abbreviated as RE), also translated into regular expression, regular expression, is a character pattern used to match specified characters in the search process.
Programs that support regular expressions such as: find| vim| grep| sed |awk
2.2 Application scenarios of regular expressions
1. Match email, match ID number, mobile phone number, bank card number, etc.
2. Match some specific strings, do specific processing, etc.
2.3 Regular expression noun explanation
Metacharacter
Refers to those special characters with special meaning in regular expressions, such as: dot (.) star (*) question mark (?), etc.
Leading character
The character before the metacharacter.ab c * aoo o.
2.4 The first type of regular expression
Common metacharacters
Metacharacter | Features | Example |
---|---|---|
. | Match any single character except newline | |
* | The leading character appears 0 times or multiple times in a row | |
.* | Match any length character | |
^ | Start of line (beginning with) | ^root |
$ | End of line (ends with) | root$ |
^$ | Match blank lines | Commonly used |
[] | Match any single character or group of single characters in brackets | [abc] |
[^] | Match does not contain any single character or a group of single characters in parentheses | [^abc] |
^[^] | Match does not start with any single character or a group of single characters in parentheses | ^[^abc] |
Other commonly used metacharacters
Metacharacter | Features | Example |
---|---|---|
\< | Take the head of the word | grep '\ <hel' 1.txt |
\> | Take the end of the word | grep ‘rld\>’ 1.txt |
\< \> | Exact match | grep ‘\<hello\>’ 1.txt |
\{n\} | Match the leading character n consecutive times | |
\{n,\} | Match the leading character at least n times | |
\{n,m\} | Match the leading character between n and m times | |
\( \) | Save the matched characters | sed ‘s/(10.1.1.).1/\1.254/g’ 1.txt |
\d | Match numbers (grep -P) | [0-9] |
\w | Match alphanumeric underscore (grep -P) | [a-zA-Z0-9_] |
\s | Match spaces, tabs, and form feeds (grep -P) | [\t\r\n] |
Commonly used extended metacharacters
grep must add -E or use egrep
sed must add -r
Extended metacharacters | Features | Example |
---|---|---|
+ | Match one or more leading characters | |
? | Match zero or one leading character | |
| | or | Match a or b |
() | Group of characters (as a whole) | (my|your)self: means match yourself or match yourself |
{n} | Leading character repeated n times | |
{n,} | The leading character is repeated at least n times | |
{n,m} | The leading character is repeated n to m times |
2.5 The second type of regular expression
Extended metacharacters | Features | Example |
---|---|---|
[: Scooping] | Alphanumeric characters | [[Charon]] + |
[:alpha:] | Alphabetic characters (including upper and lower case letters) | [[:alpha:]]{4} |
[:blank:] | Spaces and tabs | [[:blank:]]* |
[:digit:] | digital | [[:digit:]]? |
[:lower:] | Lower case letters | [[:lower:]]{4,} |
[:upper:] | uppercase letter | [[:upper:]]+ |
[:point:] | Punctuation | [[:point:]] |
[:space:] | All whitespace including line breaks, carriage returns, etc. | [[:space:]]* |