Regular Expression-awk-Usage Details
The awk command is a programming language used to process text and data in linux/unix. Data can come from standard input (stdin), one or more files, or other command output. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool in linux/unix. It is used in the command line, but more is used as a script. Awk has many built-in functions, such as arrays and functions. This is the same as the C language. Flexibility is the biggest advantage of awk. Awk syntax format: print the 1st and 2nd ends separated by colons [root @ test1 test] # awk-F': ''{print $1, $2} 'passwd | head-2 root x bin x #-F specifies the delimiter (separated by spaces by default) use custom characters to connect to each segment [root @ test1 test] # awk-F': ''ofs = "#" {print $1, $2, $3} 'passwd | head-2 root * x * 0 bin * x * 1 [root @ test1 test] # awk-F ': ''{print $1" * "$2" * "$3} 'passwd | head-2 root * x * 0 bin * x * 1 matched character or string [root @ test1 test] # awk '/root/'passwd root: x: 0: 0: root:/bin/bash operator: x: 1 1: 0: operator:/root:/sbin/nologin match the character or string and output the specified segment [root @ test1 test] # awk-F ': ''/root/{print $1, $5} 'passwd root operator print the row number and column number [root @ test1 ~] # Awk-F': ''{print NR": "NF} 'passwd # awk built-in variable NF (number of segments) NR (number of rows) print matching characters for the first section [root @ test1 ~] # Awk-F': ''$1 ~ /Oo/{print $1} 'passwd root #~ Fuzzy match = exact match print the content of the entire line matching the first or third match, and output the row number [root @ test1 ~] # Awk-F': ''$1 ~ /Oo/| $3> 500 {print NR ":" $0} 'passwd 1: root: x: 0: 0: root:/root: /bin/bas/mysqlh 23: izabbix: x: 501: 501:/home/zabbix: /sbin/nologin28374 #$0 indicates that the entire row is output. | or & gt; <>=! === [Root @ test1 ~] # Awk-F': ''nr <3 & $1 = "root" {print $0} 'passwd root: x: 0: 0: root:/root: /bin/bas/mysqlh print the value of section 7th to 3 + 4 and connect 7 with # [root @ test1 ~] # Awk-F': ''ofs = "#", $7 = $3 + $4 {print $0} 'passwd root # x #0 #0 # root #/root #0 bin # x #1 #1 # bin #/ bin #2 daemon # x #2 #2 # daemon #/sbin #4 print the accumulated value of the third column [root @ test1 ~] # Awk-F': ''{(sum = sum + $3)}; END {print sum} use the if keyword [root @ test1 ~] In 'passwd 2046 awk # Awk-F': ''{if ($1 =" root ") print $0} 'passwd root: x: 0: 0: root:/root: /bin/bas/mysqlh