当前位置:网站首页>Awk of shell script
Awk of shell script
2022-06-29 05:41:00 【One color stocks plummeted】
List of articles
Preface
What is? AWK
WK Is a language for processing text files , Is a powerful text analysis tool . It is a programming language specially designed for text processing , It's also line processing software , Usually used to scan 、 Filter 、 Count and summarize the work quantity , It can come from standard input, pipe or file
20 century 70 Was born in Bell Laboratories , Now? centos7 It's using gawk, The name AWK Because it took three founders Alfred Aho,Peter Weinberger, and l Brian Kernighan Of Family Name First character .
working principle :
When you read the first line , Matching condition , Then perform the specified action , Then read the second line of data processing , No default output , If no matching criteria are defined, the default is to match all data rows ,awk Implicit loop , The action will be executed as many times as the condition matches .
Read text line by line , The default is space or tab Key to separate the separator , Save the separated fields to the built-in variables , And execute the edit command according to the mode or condition .
sed Commands are often used for a whole line of processing , and awk They tend to divide a line into multiple "“ Field " And then deal with it ..awk Information is also read line by line , The execution result can be obtained through print To print and display field data .
In the use of awk In the course of the order , You can use logical operators ”&&“ Express " And ”、"|| Express " or "、"!“ Express non ” ; You can also do simple mathematical operations , Such as +、-、*、/、%、^ Respectively means plus 、 reduce 、 ride 、 except 、 Remainder and power .
awk Built-in variables
FS: Specify the field separator for each line of text , The default is space or tab stop
NF: Number of fields in the row currently processed
NR: Line number of the currently processed line ( Ordinal number )
$0: The entire contents of the currently processed row
$n: Of the current processing line n A field ( The first n Column )
FTLENAME: File name processed
RS: Line separator ,awk Read the reference room from the file , The price is based on RS The definition of cut data into many records ,
and awk Read only one record at a time , To deal with . The default is “\n”
Jane said : Data record separation , The default is \n, One record for each action
awk -F : '{print $1}' xy
## Print xy The first column of the text , And : Separator
awk -F : '{print $1,$2}' xy
## Print xy The first and second columns of the text , And : Separator
Separate with commas , Display with space effect
awk -F: '/home/{print $0}' xy
## The print contains home The whole line of
awk -F: '/home/{print $1}' xy
## The print contains home The first column
awk -F[:/] '{print NF}' xy
## Statistics to : and / As a separator , How many columns are there
awk -F[:/] '{print NR}' xy
## According to the line Numbers
awk ‘NR==10’ xy
## Display the entire tenth line
awk -F: 'NR==10{print $1}' xy
## With : Separator , Print the first column of the tenth row
awk -F: 'NR==10{print $NF}' xy
## Print the last column of the tenth row
awk 'END{print NR}' xy
## The statistical number of rows
ifconfig ens33 |awk '/netmask/{print $2}'
## Print this machine IP Address
ifconfig ens33| awk '/RX p/{print $5}'
## Print the current flow
Line by line what tasks to perform before starting , What tasks will be performed after the end of , use "BEGIN、END"
BEGIN Generally used for initialization , Read the data only once before
END It is generally used for summary operation , Execute only once after reading the data record
awk 'BEGIN{FS=":";OFS="---"}{print $1,$2}' xy
##OFS Define how the output is separated ,$1$2 The middle should be separated by commas , Because commas are mapped to by default OFS Variable , This variable defaults to a space
awk Arithmetic
Fuzzy matching , use ~ Means to contain , !~ Does not include
awk -F: '$1~/ro/{pring $2}' xy
## Print the first column containing ro The second column of the row , With : Separate
awk -F: '$1!~/ro/{pring $2}' xy
## The first column printed does not contain ro The second column of the row , With : Separate
awk -F: '$1=="xy"' xy
## Print... Equals the first column xy The line of
awk -F: '$3>=1000' xy
## Print the third column greater than or equal to 1000 The line of
awk -F: '$3<10 || $3>=1000' xy
## Print the third column less than 10, Or the third column is greater than 1000 The line of
awk -F: '$3>10 && $3<=1000' xy
## Print the third column greater than 10 And less than 1000 The line of
awk 'NR>2 && NR<9' xy
## Print line number is greater than 2 Less than 9 Entire line of
seq 200 | awk '$1%7==0 && $1~/7/'
## Print the first column divided by 7 Take remainder equal to 0 And the first column contains 7 The line of
awk '{print FNR,$0}' xy /etc/passwd
## Display the line numbers of the two files separately ,NR The line numbers of two texts will be displayed in order , Will not recalculate
awk 'BEGIN{RS=":"}{print $0}' xy
## Every time I meet : Just wrap and continue to show
awk 'BEGIN{ORS=" "}{print $0}' xy
## Merge multiple rows into one row , When outputting, separate each line with a space , Customizable
awk Advanced usage
awk -v b="$a" 'BEGIN{print b}'
## Set the system variables a, stay awk Assign a value to a variable b, Then call the variable b
awk 'BEGIN{print "'$a'"}'
## Direct call requires double quotation marks first and then single quotation marks
awk -vc=1 'BEGIN{print c}'
##awk Directly define and reference
Call function getline, When reading a row of data, you do not get the current row, but the next row of the current row
awk -F: '{if($3<10){print $0}}' /etc/passwd ## Print the third column less than 10 Entire line of
awk -F: '{if($3<10){print $2}else{print $5}}' xy
## The third column is less than 10, Print the second column , Otherwise, print the fifth column
awk And support for loop ,while loop , function , Array etc.
awk 'BEGIN{x=0};/\/bin\/bash$/ {x++;print x,$0};END {print x}' /etc/passwd
##x=0 Is to define variables ,/\/bin\/bash$/ In order to /bin/bash The line at the end ,x++ Add... Every time 1,print x,$0, Print... For each cycle x Value and the entire line ,END End operation final print x Indicates the total number of rows that meet the conditions or the number of cycles
grep -c "/bin/bash$" /etc/passwd ## Statistics to /bin/bash How many lines are there at the end
Ternary operator
awk -F: ‘{mak=($3>$4) ?$3:$4; {print mak}}’ xy ##$3>$4) ?$3:$4 Ternary operator , If the value of the third column is greater than the value of the fourth column , Then assign the third column to max, Otherwise, assign the fourth column to max
awk -F: ‘($1~“root”) && (NF==7) {print $0}’ /etc/passwd
## The first column contains root And the number of columns is equal to 7, Print the entire line
Through pipes 、 Double quotes call shell command
echo $PATH| awk ‘BEGIN{RS=":"};END {print NR}’
echo $PATH| awk ‘BEGIN{RS=":"};{print NR,$0};END {print NR}’
## Count the number of colon separated text paragraphs ,END{} In the block , Often put in the print results and other statements
Memory
View the current memory usage percentage
free -m |awk '/Mem:/{print int($3/($3+$4)*100)"%"}'
free -m |awk '/Mem:/{print $3/$2}'0.327869
free -m |awk '/Mem:/{print $3/$2*100}'32.7869
free -m |awk '/Mem:/{print $3/$2*100}'32
free -m |awk '/Mem:/{print $3/$2*100}'
free -m |awk '/Mem:/{print int($3/$2*100)"%"}'32
free -m |awk ‘/Mem:/ {print $3/$2*100}’ | awk -F. ‘{print $1 “%”}’
top -b -n 1 |grep Cpu|awk -F ‘,’ ‘{print $4}’ | awk ‘{print $1}’ ## View the current CPU Idle rate ,(-b -n 1 It means that you only need 1 The output of this )
date -d “$(awk -F “.” ‘{print $1}’ /proc/uptime) second ago”+"%F %H:%M:%S"
## Display the last system restart time , Equate to uptime;second ago To show how many seconds ago .
summary
One of the three Internet swordsmen awk Usage of
边栏推荐
- Embedded RTOS
- Common methods for describing 3D models of objects and their advantages and disadvantages
- An efficient flutter hybrid stack management scheme with zero intrusion, you deserve it!
- AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
- On February 15, the market hot money operation and the dragon and tiger list
- See how I do it step by step (I)
- 2022 recommended quantum industry research industry development planning prospect investment market analysis report (the attachment is a link to the online disk, and the report is continuously updated
- The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
- Private project practice sharing gtlab+jenkins architecture construction and document reference
- Manual (functional) test 1
猜你喜欢

Open source demo| you draw and I guess -- make your life more interesting

To learn more about Yibo Hongmeng development

The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology

See how I do it step by step (I)

Easy to get started naturallanguageprocessing series topic 7 text classification based on fasttext

2022 recommended quantum industry research industry development planning prospect investment market analysis report (the attachment is a link to the online disk, and the report is continuously updated

CCTV revealed that xumengtao won the black Technology: there was a virtual coach???

2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up

2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin (no2tdmpp) H2) /5,10,15,20-tetra (4-methylphenyl) porphyrin (TMPP) H2) Qiyue porphyrin products

Robot reinforcement learning - transferring end-to-end videomotor control from simulation to realworld (curl 2017)
随机推荐
Openfpga wishes you a happy Lantern Festival!
How to choose congestion model and anemia model
i-Teams W3: How to build a sound-bottling business
DANGER! V** caught climbing over the wall!
2022-2028 global and Chinese industrial digital electronic blasting detonator Market Status and future development trend
Private project practice sharing gtlab+jenkins architecture construction and document reference
Love that can't be met -- what is the intimate relationship maintained by video chat
Set column width in jitter - set column width in jitter
Tcapulusdb Jun · industry news collection (V)
Introduction to Photoshop (the first case)
5000+ 字解读 | 产品经理:如何做好元器件选型?
Signal slot mechanism
Analysis report on the investment market of the development planning prospect of the recommended NFT industry research industry in 2022 (the attachment is a link to the online disk, and the report is
Résultats D - exam de Qinhuangdao au cours des 20 dernières années
5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
patent filter
Parsing rshub document auto generation API
Sailing with karmada: multi cluster management of massive nodes
What if modstart forgets the background user or password?
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately