In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Awk is a line processor: compared to the advantages of screen processing, there is no memory overflow or slow processing when dealing with large files, and is usually used to format text information.
Awk process: each row is processed in turn, and then output
Awk command form:
Awk [- F |-f |-v] 'BEGIN {} / / {command1; command2} END {}' file
[- F |-f |-v] large parameter,-F specifies delimiter,-f calls script,-v defines variable var=value
'' reference code block
BEGIN initialization code block, initializing the code before processing each line, mainly referencing global variables and setting the FS delimiter
/ / match the code block, which can be a string or a regular expression
{} command code block containing one or more commands
Multiple commands are separated by semicolons
END ending code block, a code block that executes after each line is processed, mainly for final calculation or output ending summary information
Special points:
$0 represents the entire current line
$1 first field of each line
NF field quantity variable
NR record number per line, multi-file record increment
FNR is similar to NR, except that multi-file records are not incremented, and each file starts at 1
\ t Tab character
\ nLine feed character
Define delimiters when FS BEGIN
The record delimiter entered by RS, which defaults to the newline character (that is, text is entered on one line)
~ match, which is not an exact comparison compared with =
! ~ mismatch, imprecise comparison
= = equal to, must be all equal, accurate comparison
! = not equal to exact comparison
& & Logic and
| | Logic or
+ 1 or more when matching
/ [0-9] [0-9] + / two or more digits
/ [0-9] [0-9] * / one or more numbers
FILENAME file name
OFS output field delimiter, which is also a space by default, and can be changed to tabs, etc.
The record delimiter of the ORS output, which defaults to the newline character, that is, the processing result is also output to the screen one by one.
-F'[: # /] 'defines three delimiters
Print & $0
Print is the main command for awk to print specified content.
Awk'{print}'/ etc/passwd = = awk'{print $0}'/ etc/passwd
Awk'{print ""}'/ etc/passwd / / does not output the contents of passwd, but outputs the same number of blank lines, which further explains that awk processes text one line at a time.
Awk'{print "a"}'/ etc/passwd / / outputs the same number of a lines with only one letter a
Awk-F ":"'{print $1}'/ etc/passwd
Awk-F:'{print $1; print $2}'/ etc/passwd / / output the first two fields of each line, line by line, to further understand how to process text one line at a time
Awk-F:'{print $1 OFS= 6} 'OFS= "\ t" / etc/passwd / / output field 1, 3, 6, with tabs as delimiters
-f specify the script file
Awk-f script.awk file
BEGIN {
FS= ":"
}
{print $1} / / the effect is the same as awk-F ":"'{print $1}', except that the delimiter is specified in the code itself using FS
Awk 'BEGIN {Xerox 0} / ^ $/ {Xerox 1} END {print "I find", X, "blank lines."}' test
I find 4 blank lines.
Ls-l | awk 'BEGIN {sum=0}! / ^ d / {sum+=$5} END {print "total size is", sum}' / / calculate the file size
Total size is 17487
-F specifies the delimiter
1 means that after the delimiter is specified, the first field, the third field of $3, and\ t is the tab character.
One or more consecutive spaces or tabs are treated as a delimiter, that is, multiple spaces are treated as a space
Awk-F ":"'{print $1}'/ etc/passwd
Awk-F ":'{print $1 $3}'/ etc/passwd / / $1 is connected to the output of $3 without separation
Awk-F ":" {print $1 < 3}'/ etc/passwd / / an extra comma is added, and $1 and $3 are separated by a space
Awk-F ":"'{print $1 "" $3}'/ etc/passwd / / manually add a space separation between $1 and $3
Awk-F ":"'{print "Username:" $1 "\ t\ t Uid:" $3}'/ etc/passwd / / Custom output
Awk-F:'{print NF}'/ etc/passwd / / shows how many fields there are in each line
Awk-F:'{print $NF}'/ etc/passwd / / print out the value of the NF field of each line
Awk-F: 'NF==4 {print}' / etc/passwd / / displays rows with only four fields
Awk-F:'NF > 2 {print $0}'/ etc/passwd / / displays rows with more than 2 fields per row
Awk'{print NR,$0}'/ etc/passwd / / output the line number of each line
Awk-F:'{print NR,NF,$NF, "\ t", $0}'/ etc/passwd / / print line number, number of fields, last field value, tab, each line
Awk-F: 'NR==5 {print}' / etc/passwd / / shows line 5
Awk-F: 'NR==5 | | NR==6 {print}' / etc/passwd / / shows lines 5 and 6
Route-n | awk 'NRemote1 {print}'/ / does not display the first line
/ / match the code block
/ / Pure character matching! / / Pure character mismatch ~ / / Field values match! ~ / / Field values do not match ~ / A1 | a2 / Field values match A1 or a2
Awk'/ mysql/' / etc/passwd
Awk'/ mysql/ {print}'/ etc/passwd
Awk'/ mysql/ {print $0}'/ etc/passwd / / three instructions have the same result
Awk'! / mysql/ {print $0}'/ etc/passwd / / output lines that do not match mysql
Awk'/ mysql | mail/ {print}'/ etc/passwd
Awk'! / mysql | mail/ {print}'/ etc/passwd
Awk-F:'/ mail/,/mysql/ {print}'/ etc/passwd / / interval matching
Awk'/ [2] [7] [7] * / {print $0}'/ etc/passwd / / matches lines that start with 27 numbers, such as 27, 277, 2777.
Awk-F:'$1~/mail/ {print $1}'/ etc/passwd / / $1 is displayed only if the specified content is matched.
Awk-F:'{if ($1~/mail/) print $1}'/ etc/passwd / / same as above
Awk-F:'$1 mismatch mail / {print $1}'/ etc/passwd / /
Awk-F:'$1 print print mail | mysql/ {mail $1}'/ etc/passwd
IF statement
Must be used in {}, and the comparison is expanded with ()
Awk-F:'{if ($1~/mail/) print $1}'/ etc/passwd / / abbreviation
Awk-F:'{if ($1~/mail/) {print $1}}'/ etc/passwd / / write all
Awk-F:'{if ($1~/mail/) {print $1} else {print $2}}'/ etc/passwd / / if...else...
Conditional expression
= =! > > =
Awk-F ":"'$1percent = "mysql" {print $3}'/ etc/passwd
Awk-F ":"'{if ($1mm = "mysql") print $3}'/ etc/passwd / / same as above
Awk-F ":" $1percent = "mysql" {print $3}'/ etc/passwd / / is not equal to
Awk-F ":" $3 > 1000 {print $3}'/ etc/passwd / / greater than
Awk-F ":" $3 > = 100 {print $3}'/ etc/passwd / greater than or equal to
Awk-F ":" $38 {print}'/ etc/passwd / / Logic matches mail with $1, and $3 > 8
Awk-F:'{if ($1~/mail/ & & $3 > 8) print}'/ etc/passwd
Awk-F:'$1~/mail/ | | $3 > 1000 {print}'/ etc/passwd / / logical or
Awk-F:'{if ($1~/mail/ | | $3 > 1000) print}'/ etc/passwd
Numerical operation
Awk-F:'$3 > 100' / etc/passwd
Awk-F:'$3 > 100 | | $3
< 5' /etc/passwd awk -F: '$3+$4 >200'/ etc/passwd
Awk-F:'/ mysql | mail/ {print $3 million 10}'/ etc/passwd / / the third field plus 10 printing
Awk-F:'/ mysql/ {print $3murf 4}'/ etc/passwd / / subtraction
Awk-F:'/ mysql/ {print $3 times 4}'/ etc/passwd / / multiply
Awk'/ MemFree/ {print $2amp 1024}'/ proc/meminfo / / Division
Awk'/ MemFree/ {print int ($2y1024)}'/ proc/meminfo / / rounding
Output delimiter OFS
Awk'$6 ~ / FIN/ | | NR==1 {print NR,$4,$5,$6} 'OFS= "\ t" netstat.txt
Awk'$6 ~ / WAIT/ | | NR==1 {print NR,$4,$5,$6} 'OFS= "\ t" netstat.txt
/ / output field 6 matches the line of WAIT, where the line number of each line is output, and the field is split with tabs.
Output the processing results to a file
① outputs route-n directly in the command code block | awk 'NRRDB1 {print > ". / fs"}'
② uses redirection to output route-n | awk 'NRZ output 1 {print}' >. / fs
Formatted output
Netstat-anp | awk'{printf "%-8s%-8s%-10s\ n", $1meme 2 meme 3}'
Printf representation format output
% format output delimiter
-8 is 8 characters in length
S represents the type of string
Print the first three fields of each line, specifying the first field output string type (length 8) and the second field output string type (length 8)
The third field outputs the string type (length 10)
Netstat-anp | awk'$6 seconds = "LISTEN" | | NR==1 {printf "%-10s%-10s%-10s\ n", $1 for 2meme 3}'
Netstat-anp | awk'$6 seconds = "LISTEN" | | NR==1 {printf "%-3s%-10s%-10s%-10s\ n", NR,$1,$2,$3}'
IF statement
Awk-F:'{if ($3 > 100) print "large"; else print "small"}'/ etc/passwd
Small
Small
Small
Large
Small
Small
Awk-F: 'BEGIN {Amusement Bour0} {if ($3 > 100) {Aguilar; print "large"} else {print A, "small"}} END {print A, "\ t", B}' / etc/passwd
/ / ID is greater than 100m A plus 1, otherwise B plus 1
Awk-F:'{if ($3100? "yes": "no")}'/ etc/passwd
Awk-F:'{print ($3 > 100? $3 ":\ tyes": $3 ":\ tno")}'/ etc/passwd
While statement
Awk-F: 'BEGIN {iTun1} {while (I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.