what is a regular expression?
Regular expressions are a grammatical rule that describes the pattern of character arrangement and matching. It is used primarily for pattern segmentation, matching, finding, and substitution of strings.
two, regular expressions and wildcard characters1. Regular expressions
Used to match a qualifying string in a file, the regular expression is "include match". grep, awk, sed, and other commands can support regular expressions.
2. Regular expression meta-characters
A regular expression is a string match by means of a meta character, please refer to: http://www.cnblogs.com/refine1017/p/5011522.html
3. Wildcard characters
Used to match the qualified file name, and the wildcard character is "exact match." LS, find, CP These commands do not support regular expressions, so they can only be matched using the shell's own wildcard character.
4. Wildcard characters include
* Match any character
? Match any one character
[] matches any one of the characters in brackets
Three, Cut command
The cut command cuts bytes, characters, and fields from each line of the file and writes those bytes, characters, and fields to standard output.
1. Common parameters
- B: split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
- C: split in characters.
- D: custom delimiter, default is tab.
- F: used with-D to specify which area to display.
- N: cancels splitting multibyte characters. Used only with the-B flag.
2. Example 1: Print a row of a tab-delimited file
Cat student.txt ID Name Gender Mark1 Ming F 2 Zhang F - 3 Wang m 4 li m4 student.txt Mark
3. Example 2: Print a row of a CSV file
[[email protected] shell]# Student.csv id,name,gender,mark 1 , Ming,f, 85 2 , Zhang,f,70 3 , Wang,m,75 4 , Li,m,90 [[email protected] shell]# cut -D , -F 4 Student.csv Mark 85 70 75 90
4. Example 3: Print the first character of a string
Echo " abcdef " Cut 3 C
5. Example 4: Intercept a text in Chinese characters
[Email protected] shell]#Echo "Shell Programming"|Cut-nb1S[[email protected] shell]#Echo "Shell Programming"|Cut-nb2H[[email protected] shell]#Echo "Shell Programming"|Cut-nb3E[[email protected] shell]#Echo "Shell Programming"|Cut-nb4L[[email protected] shell]#Echo "Shell Programming"|Cut-nb5L[[email protected] shell]#Echo "Shell Programming"|Cut-nb8compilation [[email protected] shell]#Echo "Shell Programming"|Cut-nb OneProcess
Iv. printf Command 1. Command format
printf ' output type output format ' output content
2. Output Type
%ns: output string. N for output of several characters, n ellipsis for all characters
%ni: output integer. n refers to the output of several numbers, n ellipsis represents all numbers
%m.nf: output floating-point number. M and n are numbers that refer to the integer and decimal digits of the output. For example, the%8.2f represents a total output of 8 digits, of which 2 bits are small trees and 6 bits are integers.
3. Output format
\a: Output Warning sound
\b: output backspace (Backspace)
\f: Clear Screen
\ n: line break
\ r: Enter (enter)
\ t: Horizontal output BACKSPACE key
\v: vertical output BACKSPACE key
4. Example
[[email protected] ~]# printf'%i%s%i%s%i\n' 1 "+" 2 "=" 31+2=3[[Email protected]~]# printf'%i-%i-%i%i:%i:%i\n' - A 3 + About - -- A-3 +: About: -
V. awk Command 1. Command format
awk ' Condition 1{action 1} Condition 2{action 2} ... ' Filename
Condition: general use of relational expressions as conditions, such as x > 10
actions: formatted output, Process Control statements
2. Example 1: Extracting a row of a tab-delimited file
[Email protected] shell]#Catstudent.txt ID Name Gender Mark1Ming F -2Zhang F -3Wang M the4Li M -[email protected] shell]#awk '{print ' \ t ' $4}'student.txt ID Mark1 -2 -3 the4 -
3. Example 2: Get Disk utilization
[Email protected] shell]#DF-hfilesystem Size used Avail use%mounted on/dev/sda2 18G2.4G 14G the%//dev/sda1 289M 16M 258M6% /Boottmpfs 411M0411M0%/dev/Shm[[email protected] shell]#DF-H |grep "sda1"|awk '{print $}'6%
Vi. sed command
SED is a lightweight flow editor that is almost included on all UNIX platforms, including Linux. SED is primarily used to select, replace, delete, and add commands to the data.
1. Command format
sed [options] ' [action] ' file name
2. Options
- N: The general sed command will output all data to the screen, and if you join this option, only the lines processed by the SED command will be output to the screen.
- E: allows multiple sed command edits to be applied to the input data.
-I : directly modifies the file that reads the data with the result of the SED modification, rather than the screen output.
3. Action
A: append, add one or more rows after the current line
C: line substitution, replacing the original data row with the string following C
I: insert, insert one or more rows before the current line.
d: Delete, delete the specified row
p: print, output the specified line
s: string substitution, replacing another string with one string. The format is "line range/s/old string/new string/g" (similar to the replacement format in vim)
4. Example
[Email protected] shell]#Catstudent.txt ID Name Gender Mark1Ming F -2Zhang F -3Wang M the4Li M90
#测试-n parameter [[email protected] shell]#sed-N'2p'Student.txt1Ming F85
#测试单行删除 [[email protected] shell]#sed '2d'student.txt ID Name Gender Mark2Zhang F -3Wang M the4Li M90
#测试多行删除 [[email protected] shell]#sed '2,4d'student.txt ID Name Gender Mark4Li M90
#测试追加 [[email protected] shell]#sed '2a test Append'student.txtid Name Gender Mark1Ming F -Test Append2Zhang F -3Wang M the4Li M90
#测试插入 [[email protected] shell]#sed '2i Test Insert'student.txtid Name Gender marktest Insert1Ming F -2Zhang F -3Wang M the4Li M90
#测试行替换 [[email protected] shell]#sed '2c test Replace'student.txtid Name Gender marktest Replace2Zhang F -3Wang M the4Li M90
#测试内容替换 [[email protected] shell]#sed '2s/ming/replace/g'student.txtid Name Gender Mark1Replace F -2Zhang F -3Wang M the4Li M -
Shell Learning notes-Regular expressions