Shell Study Notes (2) Regular Expressions in shell and shell Regular Expressions
The difference between a wildcard and a regular expression is the native syntax supported by bash. A regular expression is a way to process strings. A regular expression must be supported by a tool.
Language family settings: export LANG = Cgrep alias settings: grep -- color = auto
Some advanced parameters of grep
Grep [-A] [-B] 'search FOR THE string' filename // output both the previous k row and the subsequent m row-A: numbers can be added to the end, indicates to list the modified rows and the next n rows-B: to list the first few rows
If the regular expression is used in the search keyword, the regular expression must be added with [] eg: grep '[a-z] oooo' filename.
Match the first line '^ string', but' [^ s] string' indicates to exclude the letter s from matching the end of the line 'string $ 'eg: exclude the comment line, and empty line grep-v '^ $'/etc/profile | grep-v-n' ^ [[: blank:] * #'
Sed usage
Sed [-nefr] [action] sed can operate on the file directly. Options and parameters:-n: Use 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 (or action) that has been specially processed by sed will be listed. -E: directly edit the sed action in the Command column mode.-f: Write the sed action in a file.-f filename can run the sed action in filename; -r: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)-I: directly modify the content of the file to be read, rather than output by the screen. Action Description: [n1 [, n2] functionn1, n2: does not necessarily exist. Generally, it indicates "number of rows selected for action". For example, if my actions need to be performed between 10 and 20 rows, then the "10, 20 [Action Behavior]" function has the following Doon: a: new, and a can be followed by a string, these strings will appear in the new line (the current next line )~ C: replace. c can be followed by strings. These strings can replace rows between n1 and n2! D: delete, because it is delete, so d is usually not followed by any comment; I: insert, I can be followed by a string, these strings will appear in the new line (the previous line currently); p: print, or print the selected data. Generally, p runs with the sed-n parameter ~ S: replace, you can directly replace the work! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g!
Eg: nl regular.txt | sed '2, 5d '// Delete the second to fifth rows. The action following sed must be expanded using ''.
Nl regular | sed '2a add hello' add data after the second row
Nl regular | sed '2, 5c change 2 to 5 line content' replace 2-5 lines of content
Nl regular | sed-n' 2, 5' selected rows are printed
Sed search and replace sed's/string to be replaced/New String/G'
Cat. bashrc | sed's /#. * $ // G' | sed's/^ $ // G' | grep-n 'exp '// Delete comment rows and blank rows
Sed-I's/\. $ /\! /G' regular // directly modify the file
Awk easy-to-use data processing tools
Suitable for Small Data Processing
Awk 'condition type 1 {Action 1} Condition Type 2 {Action 2}... 'filename
Wildcard
Special regular expressions
Regular Expression
Extended Regular Expressions