9.1 Regular Introduction grep (UP)
Regular is a string of regular strings. In many text editors or other tools, regular expressions are often used to retrieve and/or replace text content that conforms to a pattern.
In fact, the regular expression is just a kind of thought, a kind of representation method. As long as the tools we use support this idea, the tool can handle the strings of regular expressions, and the common tools are grep, sed, awk.
grep syntax:
grep [-CINVABC] ' word ' filename
-C: Print the number of lines that meet the requirements
-I: Ignore case
-R: Traverse all subdirectories
-N: Output with the same number of lines as required
-V: Print rows that do not meet the requirements
-A: followed by a number (with or without spaces), for example, –A2 to print the line that meets the requirements and the following two lines
-B: followed by a number, such as –B2, to print the line that meets the requirements and the above two lines
-C: followed by a number, such as –C2, to print the line that meets the requirements and two rows above and below
9.2/9.3 grep (middle) (bottom)
Grep/egrep example
grep-n ' root '/etc/passed-----> filter out all rows containing root
Grep-nv ' nologin '/etc/passwd-----> Filter out all rows that do not contain nologin
grep ' [0-9] '/etc/inittab-----> filter out all rows that contain numbers
Grep-v ' [0-9] '/etc/inittab-----> Filter out all rows that do not contain numbers
Grep-v ' ^ # ' test.txt-----> Remove All lines beginning with ' # '
grep-v ' ^# ' test.txt|grep-v ' ^$ '-----> Remove All lines beginning with ' # ' and blank line
grep ' ^[^a-za-z] ' test.txt-----> filter out all lines that do not start with a-za-a characters
grep ' r.o ' test.txt-----> filter the rows with an arbitrary character between R and O, where . Represents any one character
grep ' oo* ' test.txt----->\ to filter out lines containing 0 or more oo characters
grep '. * ' test.txt----->. Include all 0 or more arbitrary characters (including blank lines) Line filter OUT * * * * * *
grep ' o{2} '/etc/passwd-----> filter out a line containing two o ' oo '
egrep ' o{2} '/etc/passwd-----> filter out contains two o ie ' oo ' Line
Egrep ' o+ '/etc/passwd-----> Filter lines containing one or more O strings
Egrep ' oo? '/etc/passwd-----> Filter rows containing 0 or one oo string
Egrep ' root|nologin '/etc/passwd-----> filter out rows containing root or nologin strings
Egrep ' (oo) {2} '/etc/passwd-----> Filter out rows that contain 2 oo strings
Note: ^ put on [] outside and inside meaning is different, put outside the expression with. It is not intended, for example [^a] to denote characters other than the a character.
2018-1-15 Linux Learning Notes