当前位置:网站首页>2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
2022-07-01 05:25:00 【paul__ george】
awk
1, Pre instruction | awk Options Conditions Instructions
2,awk Options Conditions Instructions Processed documents
Options -F Define separator
Instructions print
Conditions / character string /
Built-in variables $1 First column $2 Second column $3 The third column …
$0 All columns NR Line number NF Column number
[[email protected] opt]# cat abc.txt # Prepare the material
hello the world
welcome to beijing
awk ‘{print}’ abc.txt # Output all
awk ‘/to/{print}’ abc.txt # The output is to Of course
awk ‘{print $2}’ abc.txt # Output the... Of all lines 2 Column
awk ‘/to/{print $1}’ abc.txt # The output is to The second line of the line 1 Column
awk ‘{print $0}’ abc.txt # Output all rows, all columns
awk ‘{print $0,$1}’ abc.txt # Output all rows, all columns , The first 1 Column
awk ‘{print NR}’ abc.txt # Output the line numbers of all lines
awk ‘{print NR,$0}’ abc.txt # Output the line numbers of all lines , All columns
awk ‘{print NR,NF}’ abc.txt # Output the line numbers of all lines , Column number ( How many columns )
awk ‘/^bin/{print NR}’ user # Find out bin Beginning line , Displays the line number of the line
awk ‘/^bin/{print NR,$0}’ user # Find out bin Beginning line , Displays the line number of the line , All columns
awk ‘{print NF}’ user # Output the column numbers of all rows ( There are several columns in each row )
awk -F: ‘{print $1}’ user # If there are no spaces in the document , It can be used F Modify the separator
awk -F: ‘{print $1,$6}’ user # Use colons as column separators , According to the first 1、6 Column
awk -F: ‘{print $1" Our home directory is "$6}’ user # You can also output constants , Just add double quotation marks
awk -F: ‘{print $1" The interpreter is "$7}’ user
Collect the remaining capacity of the root partition
df -h | awk ‘//$/{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲' # Use df -h …/{print “ The remaining capacity of the root partition is ”$4}’ # Then add a constant to output
Collect network card traffic information
ifconfig eth0 | awk ‘/RX p/{print “eth0 The amount of data received by the network card is ”$5" byte "}’
ifconfig eth0 | awk ‘/TX p/{print “eth0 The amount of data sent by the network card is ”$5" byte "}’
awk Conditions
1./ character string / You can also use regular ~ contain !~ It doesn't contain
awk -F: ‘$6~/root/{print}’ user # Output No 6 Column contains root The line of
awk -F: ‘$6~/bin/{print}’ user # Output No 6 Column contains bin The line of
awk -F: ‘$6!~/bin/{print}’ user # Output No 6 Column does not contain bin The line of
2. Use numbers or strings
== != > >= < <=
awk -F: ‘$3<3{print}’ user # Output No 3 Less than column 3 The line of
awk -F: ‘$3<=3{print}’ user # Output No 3 Column is less than or equal to 3 The line of
awk -F: ‘NR==2{print}’ user # Output No 2 That's ok
awk -F: ‘NR>2{print}’ user # The output line number is greater than 2 The line of
awk When to deal with it , Can perform additional tasks
BEGIN Mission perform 1 Time , Execute before reading the document
Line by line task perform n Time , Execute when reading a document
END Mission perform 1 Time , Execute after reading the document
utilize awk When to deal with it , Output the following 

awk ‘BEGIN{print “User\tUID\tHome”}’ # The first 1 Step output header information
awk -F: ‘{print $1"\t"$3"\t"$6}’ user # The first 2 Step output content
awk ‘END{print “ A total of "NR" That's ok ” }’ user # The first 3 End of step output
awk -F: ‘BEGIN{print “User\tUID\tHome”}{print $1"\t"$3"\t"$6}END{print “ A total of "NR" That's ok ”}’ user # Write together
Use awk Array +for Loop to achieve advanced search
Array name [ Subscript ]= The value of the subscript
for( Variable name in Array name ){print Variable name } # This format can view all subscripts of the array
/var/log/httpd/access_log Check the records of accessing the website log 
awk ‘{a[$1]++}END{for(i in a){print i,a[i]}}’ shu.txt # Collect documents using line by line tasks and arrays shu.txt Information in , And then in END Use... In tasks for Loop through all arrays a Subscript and value of
-n Is sorted in numerical form ,-r It's in descending order , -k Yes, specify which column to sort
awk ‘{ip[$1]++}END{for(i in ip){print i,ip[i]}}’ /var/log/httpd/access_log | sort -nr -k 2 # Use sort Command to add sorting function ,-n Is sorted in numerical form ,-r It's in descending order , -k Yes, specify which column to sort
/var/log/secure Is the security log , If someone enters the wrong password when logging in
The information will be recorded , This information can be used awk Grab it out , The method is as follows :
Statistics of access in the security log root The account password is entered incorrectly ip Address and number
边栏推荐
- Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
- Unity drags and modifies scene camera parameters under the editor
- LevelDB源码分析之memtable
- Summary of spanner's paper
- Use of STM32 expansion board temperature sensor and temperature humidity sensor
- Programmers dig "holes" to get rich: if they find a loophole, they will be rewarded 12.72 million yuan
- Precautions for use of conductive slip ring
- el-cascader回显失败;el-cascader回显不出来
- Vmware workstation network card settings and three common network modes
- Data consistency between redis and database
猜你喜欢

Practice of combining rook CEPH and rainbow, a cloud native storage solution

tar命令

Usage and principle of synchronized

小程序常用组件小结

Rainbow combines neuvector to practice container safety management

Actual combat: gateway api-2022.2.13

Application and principle of ThreadPoolExecutor thread pool

複制寶貝提示材質不能為空,如何解决?

Daily question -leetcode1175- permutation of prime numbers - Mathematics

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
随机推荐
Use and principle of Park unpark
Go learning notes (5) basic types and declarations (4)
Receiving package install and uninstall events
Principle, technology and implementation scheme of data consistency in distributed database
Explanation of characteristics of hydraulic slip ring
[Yugong series] February 2022 Net architecture class 005 ABP vNext Net core web application getting started configuration
Causes of short circuit of conductive slip ring and Countermeasures
And search: the suspects (find the number of people related to the nth person)
Vmware workstation network card settings and three common network modes
Like cloud functions
Is there any good website or software for learning programming? [introduction to programming]?
How to select conductive slip ring material
C# wpf 使用DockPanel实现截屏框
Use and principle of AQS related implementation classes
Global and Chinese market of metal oxide semiconductor field effect transistors 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of superconductor 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 888. Finding combinatorial number IV (the problem of finding combinatorial number with high precision)
如何开始学剪辑?零基础详细解析
El cascade echo failed; El cascader does not echo
Intelligent operation and maintenance: visual management system based on BIM Technology