Introduction to Regular expressions
A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters. Given a regular expression and another string, we can achieve the purpose: whether the given string conforms to the filter logic of the regular expression (called "match"); You can get the specific part we want from the string by regular expressions
Second, grep introduction
Summary: grep (global Regular expression Print, globally regular expressions printing), a text filtering tool
Function: Filters out the specified line
Note: The filter for grep is in "line" units
Three, grep parameters
-C outputs only the count of matching rows (that is, how many rows match)
-A additional rows are displayed in addition to the output matching line
-B Additional lines are displayed in front of the output matching line
-C additional lines are displayed before and after the output matching line
-V Displays all lines that do not contain matching text
-r filter directories with matching characters
-N Displays the line number of the row that matches the character
-E Switch to Egrep
-Q only as a judgment without outputting results
--color Highlighting matching characters
grep ' root '/etc/passwd Filter out rows with root Root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
grep-v ' root '/etc/passwd Filter out rows with no root bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/ Sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/sync ....
Iv. rules expressions for grep
^: Anchor line starts as: ' ^grep ' matches all lines that begin with grep
$: Anchors the end of the line as: ' grep$ ' matches all rows ending with grep
^$: Represents a blank line, ^ represents the beginning of the line, $ for the end of the row, ^$ meaning is the end of the line after the beginning, there is nothing in the middle, so the empty line
^.*$: Represents all Rows
. : Matches any one character, such as ' R.P ' matches r followed by an arbitrary character, then P
*: Match 0 or more * numbers before the word such as: ' A*b ' can match B, CB, DB, 3b, $b (front is 0 a), AB (1 a), AAB (2 a), Aaaab, Aaaaaaaaaab
. *: Represents any character, such as ' a.*b ', if the beginning is a, the string ending with B can match, how many characters in the middle can be
[]: Matches characters within a specified range, such as ' [Gg]rep ' matches grep and grep
^[]: With ... Lines that begin with, such as ^[a], represent lines that begin with a
[^]: inverse, such as [^0-9] matches characters that are not numbers
^[^]: Takes a row that is not a character, such as ^[^0-9] to match a line that does not begin with a number
Extensions: grep exercises
Filter out both ABC and DEF lines: grep ' abc ' filename |grep ' def '
Filter out lines with ABC or def: grep ' abc\|def ' filename
Filter out lines that start with a or B: grep-e ' ^a|^b ' filename
Filter out lines that begin with a number: grep ' ^[0-9] ' filename
Filter out rows with numbers: grep ' [0-9] ' filename
Filter out the line with Root and lose the travel number: grep ' root ' filename-n
Filter out the line without root and lose the travel number: grep ' root ' filename-v-n
Regular Expressions: grep