Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the AWK command in linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the AWK command in linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

AWK of the linux command

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 It is further explained that awk is a line processing the text awk'{print "a"}'/ etc/passwd / / with the same number of a lines, with only one a letter 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 the text awk-F:'{print $1 line by line processing text OFS= "\ t" / etc/passwd / / output field 1line 3P 6, with tabs as delimiters

-f specify the script file

The effect of awk-f script.awk fileBEGIN {FS= ":"} {print $1} / / is the same as that of awk-F ":"'{print $1} 'except that the delimiter uses FS to specify awk' BEGIN {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/passwdawk-F ":"'{print $1 $3}'/ etc/passwd / / $1 is connected to the output of $3 without separating awk-F ":"'{print $1 camera 3}'/ etc/passwd / / has an extra comma Awk-F ":"'{print $1 "" $3}'/ etc/passwd / / manually add spaces between $1 and $3 to separate awk-F ":"'{print "Username:" $1 "\ t\ t Uid:" $3}'/ etc/passwd / / Custom output awk-F:'{print NF}'/ etc/passwd / / display how many fields there are in each row: awk-F:'{print $NF}'/ etc/passwd / / print out the value of the NF field of each line awk-F: 'NF==4 {print}' / etc/passwd / / display the line awk-F:'NF > 2 {print $0}'/ with only four fields. Etc/passwd / / displays rows with more than 2 fields per row awk'{print NR $0}'/ etc/passwd / / print the line number of each line awk-F:'{print NR,NF,$NF, "\ t", $0}'/ etc/passwd / / Number of fields, last field values, tabs, awk-F for each line: 'NR==5 {print}' / etc/passwd / / displays line 5 awk-F: 'NR==5 | | NR==6 {print}' / etc/passwd / / displays lines 5 and 6 route-n | awk 'NRIS1 {print}' / / does not show 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/passwdawk'/ mysql/ {print}'/ etc/passwdawk'/ mysql/ {print $0}'/ etc/passwd / / the result of the three instructions is the same awk'! / mysql/ {print $0}'/ etc/passwd / / output does not match the line awk'/ mysql of mysql | mail/ {print}'/ etc/passwdawk'! / mysql | mail/ {print}'/ etc/passwdawk-F:'/ mail/ / mysql/ {print}'/ etc/passwd / / interval matching awk'/ [2] [7] [7] * / {print $0}'/ etc/passwd / / matches lines that start with 27 numbers For example, 1~/mail/ 277, 2777. Awk-F:'$1~/mail/ {print $1}'/ etc/passwd / / $1 matches the specified content to display awk-F:'{if ($1~/mail/) print $1}'/ etc/passwd / / the same as above awk-F:'$1 matching print $1}'/ etc/passwd / / mismatch awk-F:'$1 matching accountablesmail | mysql/ {print $1}'/ etc/passwd

IF statement

Must be used in {}, and the comparison is expanded with ()

Awk-F:'{if ($1~/mail/) print $1}'/ etc/passwd / / abbreviated awk-F:'{if ($1~/mail/) {print $1}}'/ etc/passwd / / fully written 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 ($1percent = "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 / / logical and 1 matches mail, and $3 > 8awk-F:'{if ($1~/mail/ & & $3 > 8) print}'/ etc/passwdawk-F:'$1~/mail/ | | $3 > 1000 {print}'/ etc/passwd / / logic 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/passwdawk-F:'/ mysql | mail/ {print $3 million 10}'/ etc/passwd / / add 10 to the third field to print awk-F:'/ mysql/ {print $3 hands 4}'/ etc/passwd / / subtract awk-F:'/ mysql/ {print $3 percent 4}'/ etc/passwd / / multiply awk'/ MemFree/ {print $2max 1024}'/ proc/meminfo / / divide awk'/ MemFree/ {print int ($2ax 1024)}'/ proc/meminfo / / round

Output delimiter OFS

Awk'$6 ~ / FIN/ | | NR==1 {print NR,$4,$5,$6} 'OFS= "\ t" netstat.txtawk' $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'$6seconds = "LISTEN" | | NR==1 {printf "%-10s%-10s%-10s\ n", $1 awk'$6seconds = "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/passwdsmallsmallsmalllargesmallsmallawk-F: 'BEGIN {if ($3 > 100) {Aguilar; print "large"} else {bonding + Print "small"}} END {print A, "\ t", B}'/ etc/passwd / / ID is greater than 100 etc/passwd A plus 1, otherwise B plus 1awk-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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report