Laruence's linux private house dish-Chapter 1 Regular Expression and file formatting, linux private house dish
12.1 what is a regular expression
A regular expression is a string processing method. It processes strings in the unit of behavior. A regular expression is assisted by some special symbols, allows you to easily find, delete, and replace a specific string.
Vi, grep, awk, and sed support regular expressions, while commands such as cp and ls can only use wildcards of bash.
12.2 basic Regular Expressions
Grep advanced parameters:
Grep [-A] [-B] [-- color = auto] 'string' filename
-A: after indicates that n rows are listed in addition to the row list.
-B: before. In addition to listing this row, the preceding n rows are also listed.
-- Color = auto: you can list the colors of the correct selected data.
Basic Regular Expression exercises:
Example 1: Search for a specific string
Grep-n 'regular_express.txt
Grep-vn 'the 'regular_express.txt (-v reverse selection)
Example 2: Use brackets [] to search for character sets
Grep-n't [AE] st'regular_express.txt (matching test or tast)
Grep-n '[^ g] oo' regular_express.txt (g characters are not allowed before oo)
Grep-n' [^ [: lower:] oo 'regular_express.txt ([: lower:] indicates the meaning of a-z)
Example 3: start and end of a line
Grep-n '^ test' regular_express.txt
(Note: ^ indicates "reverse selection" in [], and "outside [] indicates positioning at the beginning of the line)
Grep-n' \. $ 'regular_express.txt (find the row whose end of the trip is the decimal point)
Example 4: any character. Duplicate character *
Grep-n 'G .. d' regular_express.txt (can match characters such as good and gglad)
Grep-n 'ooo * 'regular_express.txt (matching at least two characters above o)
Grep-n 'G. * G' regular_express.txt (find the string starting with g and ending with g,. * Indicates o or multiple arbitrary characters)
Example 5: Limit the continuous RE character range {}
Grep-n 'o \ {2 \} 'regular_express.txt (find the strings of two o)
Grep-n 'go \ {2, 5 \} G' regular_express.txt (g has two to five o s, and then receives a g String)
Grep-n 'go \ {2, \} G' regular_express.txt (g has two or more o, and then a string of g)
Basic Regular Expression characters:
^ Word: the string to be searched (word) at the beginning of the line
Word $: string to be searched (word) at the end of a row
.: It must contain any character.
\: Escape characters to remove the special meanings of special symbols
*: Repeats the first 0 or more characters.
[List]: Find the desired character from the RE characters in the character set.
[N1-n2]: Find the range of characters to be selected from the RE Character Set
[^ List]: Find a character string or range that is not expected from the RE characters in the character set.
\ {N, m \}: The first RE character from n to m in a row. \ {n \} indicates n consecutive, \ {n, \} indicates n or more consecutive
Sed tool: (See sed & awk)
Format: sed [-nefr] [action]
Parameters:
-N: Quiet Mode. In general sed usage, all data from STDIN is usually listed on the screen. However, if the-n parameter is added, only the row that has been specially processed by sed will be listed.
-E: directly edit the sed action in command line mode.
-F: Write the sed action directly in a file.-f filename can execute the sed action in filename.
-R: The expanded regular expression syntax supported by sed actions
-I: directly modify the content of the file to be read, rather than the screen output.
Action Description: [n1, [n2] function
N1 and n2 may not exist, which generally indicates the number of rows selected for the action.
Function parameters:
A: new. a can be followed by strings. These strings will appear in the current next line.
I: insert, I can be followed by strings, and these strings will appear in the previous line
C: replace. c can be followed by strings. These strings can replace rows between n1 and n2.
S: replacement, which can be directly replaced. Generally, this s can be used with a regular expression.
D: delete. Because it is delete, d is usually followed by no parameters.
P: print, that is, print the selected data. Usually p runs with the sed-n parameter.
12.3 extended Regular Expressions
+: Repeat one or more previous RE characters.
? : 0 or one of the first RE characters
|: Returns the string in the OR mode.
(): Find the string of the "group" (for example, egrep-n 'G (la | oo) D' regular_express.txt indicates to find the gglad or good string)
() +: Identify multiple duplicate groups (for example, echo 'axyzxyzxyzc' | egrep 'a (xyz) + C' to find that the start is A and the end is C, there is more than one "xyz" string in the middle)
12.4 file formatting and Processing
Format and print printf:
Format: printf'print format' actual content
There are several special styles in the format:
\ A warning sound output
\ B Return key
\ F clear screen
\ N output a new row
\ R or Enter
\ T horizontal tab button
\ V vertical tab buttons
\ XNN is a two-digit string that can be converted to a character.
Common variable formats in C Language
% Ns: n indicates a number, and s indicates a string, that is, multiple characters
% Ni: n indicates a number, and I indicates interger, that is, the number of integer words.
% N. nf: n and N are both numbers. f indicates float, for example, 10 digits. The two decimal places are % 10.2f.
Awk tool (see sed & awk for details ):
Format: awk 'condition type 1 {Action 1} Condition Type 2 {Action 2}... 'filename
Awk is mainly used to process data in fields in each row. The default field delimiter is the Space key or tab key.
Variable:
NF: Total number of fields owned by each row ($0)
NR: currently, awk processes the "nth row" of data.
FS: the delimiter. The default Delimiter is the Space key.
Logical operators:
><>==! =
Example:
Cat/etc/passwd | awk '{FS = ":" }3 3 <10 {print $1 "\ t" $3 }'
Cat pay.txt | awk '{if (NR = 1) printf "% 10 s % 10 s, % 10s \ n", $1, $4, "Total"} NR >=2 {total = $1 + $4 printf "% 10 s % 10d % 10.2f \ n", $1, $3, total }'
Text comparison tool diff:
Diff is used to compare the differences between two files, and is in the unit of behavior. diff can also compare two directories.
Format: diff [-bBi] from-file to-file
-B: Ignore the difference of multiple blank spaces in a row (for example, "about me" and "about me" are considered the same)
-B: Ignore the difference between blank rows.
-I: case insensitive
Patch-pN <patch_file update
Patch-R-pN <patch_file restore
Example: Use passwd. old and passwd. new in/tmp/test to create a patch file and update old version data.
Diff-Naur passwd. old passwd. new> passwd. patch
Update the old file to the same as the new file.
Patch-p0 <passwd. patch
(Pathing file passwd. old)
Restore the content of the old file
Patch-R-p0 <passwd. patch
File print pr:
Pr/etc/man. config (print the text file man. config)
An example of the regular expression for Private food in linux seems to be incorrect.
I:
Grep-nv '^ $'/etc/syslog. conf
-V '^ $' indicates all rows except empty rows. -N indicates the row number displayed on each line. If you do not want to display the row number, do not use the n parameter.
For example:
Grep-v '^ $'/etc/syslog. conf
II:
In the same way, this shows all the rows except for the leading. However, because you used n and added a row number before, it is useless to take the lead instead of #, but rather a number. Or remove the n parameter.
Grep-v '^ #'
Laruence's linux private dish Chapter 1 exercise questions after class
Find/etc-size + 50 k-! -User root-exec ls-ld {}\;
Find/etc-size + 50 k-! -User root-type f-exec ls-l {}\;
Either of the above two statements is acceptable! Notice! , That! It indicates reverse selection, that is, "not a project later!
Find/etc-size + 1500 k-o-size 0
That-o is or (or) relative to-!
Reference: linux.vbird.org/