Shell learning notes-Regular Expressions
I. What is a regular expression? Regular Expressions are a syntax rule used to describe character arrangement and matching modes. It is mainly used for string mode segmentation, matching, search and replacement operations. Ii. Regular Expressions and wildcards 1. Regular expressions are used to match matching strings in files. Regular Expressions are "include matching ". Commands such as grep, awk, and sed support regular expressions. 2. Regular Expression metacharacters regular expression is through metacharacters to carry on the string match, detailed reference: http://www.bkjia.com/kf/201208/147682.html 3. wildcard is used to match the qualified file name, wildcard is "exact match ". Commands such as ls, find, and cp do not support regular expressions, so they can only be matched using shell wildcards. 4. Can wildcard characters include * matching any character? Match any character [] match any character in brackets 3. cut command cut byte, character and field from each line of the file and write these byte, character and field to Standard output. 1. Common parameter-B: Split in bytes. These byte locations ignore the multi-byte character boundary unless the-n flag is also specified. -C: separated by characters. -D: custom delimiter. The default Delimiter is tab. -F: used with-d to specify the region to display. -N: undelimiter multi-byte characters. It is used only with the-B flag. 2. Example 1: print a line of the file separated by a tab
[root@localhost shell]# cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90[root@localhost shell]# cut -f 4 student.txt Mark85707590
3. Example 2: print a line of the csv file
[root@localhost shell]# cat student.csv ID,Name,Gender,Mark1,ming,F,852,zhang,F,703,wang,M,754,li,M,90[root@localhost shell]# cut -d "," -f 4 student.csv Mark85707590
4. example 3: print the character number of a string [root @ localhost shell] # echo "abcdef" | cut-c 3c5. example 4: extract a text from a Chinese character [root @ localhost shell] # echo "Shell programming" | cut-nb 1 S [root @ localhost shell] # echo "Shell programming" | cut- nb 2 h [root @ localhost shell] # echo "Shell programming" | cut-nb 3e [root @ localhost shell] # echo "Shell programming" | cut-nb 4l [root @ localhost shell] # echo "Shell programming" | cut-nb 5l [root @ localhost shell] # echo "Shell programming" | cut-nb 8 [root @ Localhost shell] # echo "Shell programming" | cut-nb 11 process 4. printf command 1. command Format: printf'output type output format' output content 2. output type % ns: Output string. N indicates the output of several characters. n indicates all characters % ni: the output integer. N indicates the number of numbers to be output. n omitted indicates all numbers % m. nf: the output floating point number. M and n are numbers, indicating the number of digits and the number of decimal places in the output. For example, % 8.2f indicates that a total of 8 digits are output, of which 2 digits are small trees and 6 digits are integers. 3. output Format \ a: Output warning sound \ B: Output Backspace \ f: clear screen \ n: line feed \ r: Press Enter \ t: horizontal output backspace key \ v: vertical output backspace key 4. example [root @ localhost ~] # Printf '% I % s % I \ n' 1 "+" 2 "=" 31 + 2 = 3 [root @ localhost ~] # Printf '% I-% I: % I \ n' 2015 12 3 21 56 302015-12-3 21:56:30 v. awk command 1. command Format awk 'condition 1 {Action 1} condition 2 {Action 2 }... 'File name condition: relational expressions are generally used as conditions, for example, x> 10 action: Format output and Flow Control Statement 2. example 1: extract a line of a tab-separated file [root @ localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90 [root @ localhost shell] # awk '{print $1 "\ t" $4}' student.txt ID Mark1 852 703 754 90 3. example 2: Obtain disk Utilization
[root@localhost shell]# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda2 18G 2.4G 14G 15% //dev/sda1 289M 16M 258M 6% /boottmpfs 411M 0 411M 0% /dev/shm[root@localhost shell]# df -h | grep "sda1" | awk '{print $5}'6%
6. The sed command sed is a lightweight stream editor that covers almost all UNIX platforms (including Linux. Sed is mainly used to select, replace, delete, and add commands for data. 1. command Format sed [Option] '[action]' file name 2. option-n: Generally, the sed command outputs all the data to the screen. If this option is added, only the rows processed by the sed command are output to the screen. -E: allows you to apply multiple sed commands to the input data. -I: Use the sed Modification result to directly modify the file for reading data, rather than the screen output. 3. action a: append, add one or more rows after the current row c: Replace the Row c, replace the original data row I: insert with the string following c, insert one or more rows before the current row. D: Delete, delete the specified row p: print, output the specified row s: String replacement, and replace the other string with one string. Format: "Row range/s/old string/New String/g" (similar to the replacement format in vim) 4. Example
[Root @ localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90 # Test-n parameter [root @ localhost shell] # sed-n '2pa' student.txt 1 ming F 85 # test single row deletion [root @ localhost shell] # sed '2d 'student.txt ID Name Gender Mark2 zhang F 703 wang M 754 li M 90 # test multiple row deletion [root @ localhost shell] # sed '2, 4d 'student.txt ID Name Gender Mark4 li M 90 # test append [root @ localhost shell] # sed '2a test append' student.txt ID Name Gender Mark1 ming F 85 test append2 zhang F 703 wang M 754 li M 90 # test insertion [root @ localhost shell] # sed '2i test insert' student.txt ID Name Gender Marktest insert1 ming F 852 zhang F 703 wang M 754 li M 90 # replace [root @ localhost shell] # sed '2c test replace 'student.txt ID Name Gender Marktest replace2 zhang F 703 wang M 754 li M 90 # replace test content [root @ localhost shell] # sed '2s/ming/replace/G' student.txt ID Name Gender Mark1 replace F 852 zhang F 703 wang M 754 li M 90