当前位置:网站首页>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 3D design and modeling software 2022-2028: Research Report on technology, participants, trends, market size and share
- Variable binding and deconstruction for rudimentary rust
- Explanation of characteristics of hydraulic slip ring
- Global and Chinese markets of gps/gnss receiver modules 2022-2028: Research Report on technology, participants, trends, market size and share
- Detailed explanation of distributed global unique ID solution
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- tar命令
- 积分商城游戏能够给商家带来什么?怎么搭建积分商城?
- Global and Chinese market of high-end home theater 2022-2028: Research Report on technology, participants, trends, market size and share
- [data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array
猜你喜欢
Detailed explanation of set
Explanation of characteristics of hydraulic slip ring
複制寶貝提示材質不能為空,如何解决?
Design and application of immutable classes
Vmware workstation network card settings and three common network modes
Precautions for use of conductive slip ring
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
[RootersCTF2019]babyWeb
导电滑环使用的注意事项
C# wpf 使用DockPanel实现截屏框
随机推荐
Use and principle of AQS related implementation classes
AcWing 885. Find the combination number I (recursive preprocessing)
AcWing 889. 01 sequence satisfying the condition (Cartland number)
Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share
[Yugong series] February 2022 Net architecture class 005 ABP vNext Net core web application getting started configuration
Distributed transactions - Solutions
Introduction of 3D Modeling and Processing Software Liu Ligang, Chinese University of Science and Technology
Vérification simple de la lecture et de l'écriture de qdatastream
And search: the suspects (find the number of people related to the nth person)
Unity project experience summary
Spanner 论文小结
Some common commands of podman
Is there any good website or software for learning programming? [introduction to programming]?
Set集合详细讲解
Rust基础入门之变量绑定与解构
Actual combat: gateway api-2022.2.13
C WPF uses dockpanel to realize screenshot box
Application of industrial conductive slip ring
Usage and principle of synchronized
AcWing 887. Finding combinatorial number III (Lucas theorem)