当前位置:网站首页>Awk tools

Awk tools

2022-06-26 13:22:00 C chord~

Catalog

introduction

One . principle

Two . Command format

3、 ... and .awk Common built-in variables

Case study

1. Output text by line

2. Output text by field

3. Through the pipe symbol 、 Double quotes call shell command


introduction

awk Command is a programming language , Used in linux/unix Processing of text and data . It is a programming language specially designed for text processing , It's also line processing software , Usually used to scan 、 Filter 、 Statistical summary work .

One . principle

  • Read the 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 “ Not ”

  • You can also do simple mathematical operations , Such as +、-、*、/、%、^ Respectively means plus 、 reduce 、 ride 、 except 、 The remainder and the power

Two . Command format

  • awk Options   " Mode or condition   [ operation ]" file 1  file 2
  • awk -f  Script files file 1 file 2 …

3、 ... and .awk Common built-in variables

 Variable 	 explain 
FS	 Column separator , Specify the field separator for each line of text , Default to space or tab stop . And "-F" The same effect 
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 )
FILENAME	 File name processed 
RS	 Line separator ,awk When reading from a file , Based on the 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’

Case study

1. Output text by line

awk '{print}' like.txt	
# Output everything 
awk '{print $0}' like.txt
# Output everything 
awk 'NR==1,NR==3{print}' like.txt
# Output No  1~3  Row content 
awk '(NR>=1)&&(NR<=3){print}' like.txt
# Output No  1~3  Row content 

awk 'NR==1||NR==3{print}' like.txt
# Output No 1 That's ok 、 The first 3 Row content 

 

awk '(NR%2)==1{print}' 1.txt
# Output the contents of all odd lines 

awk '(NR%2)==0{print}' 1.txt
# Output the contents of all even lines 

awk '/^root/{print}' /etc/passwd
# Output to  root  Beginning line 

awk '/nologin$/{print}' /etc/passwd
# Output to  nologin  The line at the end 

awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd
# Statistics to /bin/bash  Number of lines at the end 
# Equate to  grep -c "/bin/bash$" /etc/passwd

#BEGIN Pattern representation , Before processing the specified text , It has to be executed first BEGIN Actions specified in the pattern 
#awk Reprocess the specified text , We'll do it later END Actions specified in the pattern 
#END{} In the block , Often put in the print results and other statements 

2. Output text by field

awk -F ":" '{print $3}' /etc/passwd
# Output in each line ( Separated by spaces or tab stops ) Of the 3 A field 

awk -F ":" '{print $1,$3}' /etc/passwd
# Output the 1、3 A field 

awk -F ":" '$3<5{print $1,$3}' /etc/passwd
# Output No 3 Field values are less than 5 Of the 1、3 Field contents 

 

awk -F ":" '!($3<200){print}' /etc/passwd
# Output No 3 The value of each field is not less than 200 The line of 

awk 'BEGIN {FS=":"};{if($3>=200){print}}' /etc/passwd
# I'll finish it first BEGIN The content of , And print out the contents of the text 

awk -F ":" '{max=($3>$4)?$3:$4;{print max}}' /etc/passwd
#($3>$4)?$3:$4 Ternary operator , If the first 3 The value of field is greater than 4 Values for fields , Then put the 3 The value of a field is assigned to max, Otherwise, No 4 The value of a field is assigned to max

awk -F ":" '{print NR,$0}' /etc/passwd
# Output each line content and line number , Every time a record is processed ,NR It's worth adding 1

3. Through the pipe symbol 、 Double quotes call shell command

echo $PATH | awk 'BEGIN{RS=":"};END{print NR}'
# Count the number of colon separated text paragraphs ,END{} In the block , Often put in the print results and other statements 
awk -F: '/bash$/{print | "wc -l"}' /etc/passwd
# call  wc -l  Command statistics usage  bash  Number of users , Equate to  grep -c "bash$" /etc/passwd
free -m | awk '/Mem:/ {print int($3/($3+$4)*100)}'
# View the current memory usage percentage 

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 times )

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 ,+"%F %H:%M:%S" Equate to +"%Y-%m-%d %H:%M:%S" Time format of 

awk 'BEGIN {while ("w" | getline) n++ ; {print n-2}"%"}'
# call w command , And used to count the number of online users 

awk 'BEGIN {"hostname" | getline ; {print $0}}'
# call  hostname, And output the current host name 
seq 10 | awk '{print $0; getline}'   
seq 10 | awk '{getline; print $0}'

原网站

版权声明
本文为[C chord~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261227386017.html