当前位置:网站首页>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
边栏推荐
- 如何开始学剪辑?零基础详细解析
- JS random verification code
- Mathematical knowledge: finding the number of divisors
- Unity drags and modifies scene camera parameters under the editor
- Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
- Use and principle of AQS related implementation classes
- One click deployment of highly available emqx clusters in rainbow
- Global and Chinese market of 3D CAD 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share
- AcWing 885. Find the combination number I (recursive preprocessing)
猜你喜欢

Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?

Summary of spanner's paper

Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications

Usage and principle of synchronized

Lock free concurrency of JUC (leguan lock)

Memtable for leveldb source code analysis

小程序常用组件小结

实战:redux的基本使用

Data consistency between redis and database

tar命令
随机推荐
STM32 expansion board digital tube display
Global and Chinese market of mainboard 2022-2028: Research Report on technology, participants, trends, market size and share
Flutter can refresh data every time the interface comes in
QT等待框制作
Use and principle of AQS related implementation classes
Implementation of distributed lock
0xc000007b the application cannot start the solution normally (the pro test is valid)
Summary of spanner's paper
Global and Chinese market of paper machine systems 2022-2028: Research Report on technology, participants, trends, market size and share
Like cloud functions
Unity drags and modifies scene camera parameters under the editor
AcWing 884. Gauss elimination for solving XOR linear equations
Unity项目心得总结
Go learning notes (5) basic types and declarations (4)
busybox生成的东西
使用 Nocalhost 开发 Rainbond 上的微服务应用
How to meet the requirements of source code confidentiality and source code security management
AcWing 887. Finding combinatorial number III (Lucas theorem)
FileInputStream
El cascade echo failed; El cascader does not echo