Shell basics (3)

Table of contents

One, regular expressions

1. Basic rules

2. Extended regularization

3. Common regular expressions

2. File Operation Four Musketeers

1, advanced grep

2.egrep

3, find advanced

4,sed

5,awk


One, regular expressions

1. Basic rules


    a) Find specific characters cat test.txt | grep -n 'was'
    b) Use [] to find set characters cat test.txt | grep -n 'sh[io]rt' match i or o
        cat test.txt | grep - n '[^w]' Exclude w
        cat test.txt | grep -n '[ah]oo'
        cat test.txt | grep -n '[0-9]'
    c) Find the beginning of the line "^" and the end of the line " $" 
        cat test.txt | grep -n '^[AZ]'
        cat test.txt | grep '\.$' \ is the escape character
    d) Find any character "." and repeated characters "*"
        cat test. txt | grep -n 'w..d'
        cat test.txt | grep -n 'ooo*'
    e) To find the continuous character range "{}", need to use escape character, "\{\}"
        cat test. txt | grep -n 'o\{2\}'
        cat test.txt | grep -n 'wo\{2,5\}d'
        cat test.txt | grep -n 'wo\{2,\}d'

2. Extended regularization

a) +, repeat one or more of the previous character
    cat test.txt | grep -nE 'wo+d' or
    cat test.txt | egrep -n 'wo+d'
b) ?, zero or one of the previous character Character
    cat test.txt | egrep -n 'bes?t'
c)|, use or to find multiple characters
    cat test.txt | egrep -n 'of|is|on'
d) (), find group characters String
    cat test.txt | egrep -n 't(a|e)st'
e)()+, identify multiple repeated groups
    cat test.txt | egrep -n 'A(xyz)+C'

3. Common regular expressions

3.1 Number
    "^[0-9]*[1-9][0-9]*$" //Positive integer  
    "^((-\d+)|(0+))$" //Non-positive integer (negative integer + 0)  
    "^-[0-9]*[1-9][0-9]*$" // negative integer  
    "^-?\d+$" // integer 
    "^\d+(\.\d+ )?$" //Non-negative floating point number (positive floating point number + 0)  
    "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([ 0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$” // Positive floating point number 
    "^((-\d+(\.\d+)?)|(0+(\.0+)?))$" // non-positive floating point number (negative floating point number + 0) "^( 
    - ?\d+)(\.\d+)?$” // floating point number 

3.2 String
    "^[AZ]+$" //A string composed of 26 uppercase English letters  
    "^[az]+$" //A string composed of 26 lowercase English letters  
    "^[A- Za-z0-9]+$” //A string composed of numbers and 26 English letters  
    “^\w+$” //A string composed of numbers, 26 English letters or underscores

3.3Email
    "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email address "^([  
    w -.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-] +.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$” //Email 

3.4Url 
    “^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$” //url 

3.5IP
    “^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$”   //IP地址

3.6Tel 
    /^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-)) ?([0-9]{7,8})(\-[0-9]+)?$/ //Phone number

3.7 Date verification

   /^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1- 9]{1}))|(3[0|1]))$/ // year-month-day yyyy-MM-dd / yy-MM-dd format "^[0-9]{4}-
   ( (0([1-9]{1}))|(1[1|2]))-(([0-2]([0-9]{1}))|(3[0|1] ))$" // year-month-day yyyy-MM-dd format
  /^((0([1-9]{1}))|(1[1|2]))/(([0-2 ]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/ // month/day/year

2. File Operation Four Musketeers

1, advanced grep


    options 

      -r recursively scans every file in the specified directory
        -l only displays the file name matching the specified keyword, not the file content
    Case
        view all file names containing bash in the /etc directory
        grep -rl bash /etc

2.egrep


    Perfect support for regular expressions

3, find advanced


    3.1 Search by permission -perm
    3.2 Search by timestamp
        -atime
        -mtime
        -ctime
  3.4 -exec
        find /var/spool/mail -type f -exec rm -rf {} \;
   3.5 xargs
        find /var/spool/mail -type f | xargs rm -rf

4,sed

4.1 Syntax
    sed [option] 'operation' parameter 
    sed [option] -f scriptfile parameter

4.2 Options
    -e: Indicates processing with specified command or script
    -f: Specify script file
    -h: Help
    -n: Indicates only displaying the processed result
    -i: Edit text file directly
    -r: Support extended regular

 4.3 Operation

a: Add, add the specified content in the row below the current row
c: Replace, replace the selected row
d: Delete, delete the specified row
i: Insert, insert a row above the selected row
p: Print
s: Replace, replace the specified row character
y: character conversion

5,awk

5.1 Syntax
    awk option 'pattern or condition {edit command}' file1 file2 ...
    awk -f script file file1 file2 ...

5.2 Options

-F specifies the delimiter for each line,
the default delimiter is space

5.3 Built-in variables

FS: specify the delimiter of each line
NF: specify the number of fields of the current processing line
NR: the line number of the current processing line
$0: the entire line content of the current processing line
$n: the nth field of the current processing
FILENAME: the processing file name
RS: data record separator, default is \n

5.4 Case

a) Output
    awk '{print}' test.txt by line #equivalent to cat 
    awk 'NR>=1&&NR<=3{print}' test.txt 
    awk 'NR==1,NR==3{print}' test. txt #Print 1 to 3 lines
    awk 'NR%2==0{print}' test.txt #Print even-numbered lines
b) Output by segment
    defaults to "space" segment!
    ifconfig ens33 |awk '/netmask/{print $2}' #filter IP address
    cat /etc/shadow | awk -F : '$2=="!!"{print $1}' #print users who cannot log in to the system
c) call Shell command
    cat /etc/passwd | awk -F : '/bash$/{print | "wc -l"}' /etc/passwd #Statistics of the number of users who can log in to the system    

Guess you like

Origin blog.csdn.net/mengjialiang2002/article/details/132415014