Regular expressions
In Linux, wildcard characters are interpreted by the shell, and regular expressions are interpreted by commands. The main text processing tools are grep,sed and awk.
grep matching pattern
grep accepts options and parameters as follows
grep [Options] regex [Files]
The main parameters are
-I------------ignoring case
-O------------Show only matching content
-V------------reversed, not matched
-l-------------Output matching file name
-L-------------output does not match the file name
-C-------------count output matches (number of rows)
-N-------------output match line plus line number
-a-------------output matching rows, and the following n rows
Regular characters
^------------the beginning of the line mark, in [^] represents the non-
$-------------End Tag ' abc$ '----->abc,hhhhabc
. -------------any character ' a.c '-------->ABC,ATC,AJC
| -------------or--------> ' aaa|bbb '----->AAA,BBBC
Regular range
? ----------------the first character on the left appears 0 or one time
*----------------The first character on the left appears 0 to n times
+-----------------The first character on the left appears 1 to n times
{}----------------{m}{m,n}{m,}{,n} The first character on the left appears m/m to N/>m/<n times
()--------------list the candidate elements to use | split--(ab|1|2|3)--represents AB1 ab2 or AB3
Example
grep ' root '/root/test/z.txt---------------shows the rows that contain root in z.txt
Egrep ' ^[a-z]+[0-9]+[a-z] ' z.txt-----------filter out lines of letters + numbers + letters
sed Flow editor
Sed is an online editor that processes a single line of content at a time, and can replace, delete, add, select, and more specifically work with the data
The SED command format is
sed [options] ' command ' in_file
Options
-I: Directly modify the contents of the Read file
-N: Use Quiet mode. Half of the SED usage, the file all content will be back to the screen, if you add the-n parameter, only through the SED special processing of the line will be listed.
-E: Action editing of SED directly in instruction-column mode
-R: Extending regular Expressions
Command
' [Address 1, address 2][function] [parameter] '
1: Single line
1, 3: range, from first line to third row
$: Last line
The regular must be wrapped up in//
Function
A: New, a inserted string appears on a new line.
C: Replace, C, followed by a string, replacing the line between n1,n2.
D: Delete,
I: Insert, the inserted string appears on the previous line of the destination row.
P: Print, select a line to print out, usually with sed-n. Print a fixed line
S: Replace, replace, usually used with regular expressions, e.g. 1,20s/old/new/g
String substitution:
Sed-n ' s/root/zzzz/p ' z.txt---------replace root for zzzz and print it out. If you want to replace all, add g at the end
$ Sed-n ' s/root/xxxx/2p ' z.txt----Replace no line second root for, XXXX.
17, print the first word and the third word of each line.
Regular Expressions of Linux