当前位置:网站首页>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
边栏推荐
- [RootersCTF2019]babyWeb
- Thread process foundation of JUC
- AcWing 884. Gauss elimination for solving XOR linear equations
- 3D建模與處理軟件簡介 劉利剛 中國科技大學
- printk 调试总结
- 複制寶貝提示材質不能為空,如何解决?
- Global and Chinese markets for soft ferrite cores 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
- busybox生成的东西
- Global and Chinese market of metal oxide semiconductor field effect transistors 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Daily question -leetcode1175- permutation of prime numbers - Mathematics

Actual combat: gateway api-2022.2.13

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

0xc000007b应用程序无法正常启动解决方案(亲测有效)

工业导电滑环的应用
![[RootersCTF2019]babyWeb](/img/b4/aa8f8e107a9dacbace72d4717b1834.png)
[RootersCTF2019]babyWeb

Summary of spanner's paper

Intelligent operation and maintenance: visual management system based on BIM Technology

One click deployment of highly available emqx clusters in rainbow

导电滑环短路的原因以及应对措施
随机推荐
积分商城游戏能够给商家带来什么?怎么搭建积分商城?
printk 调试总结
Redis数据库的部署及常用命令
Numeric amount plus comma; JS two methods of adding three digits and a comma to numbers; JS data formatting
1076 Forwards on Weibo
More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv. so‘.
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*
Summary of spanner's paper
Leetcode522- longest special sequence ii- hash table - String - double pointer
TypeORM 框架
Solution: drag the Xib control to the code file, and an error setvalue:forundefined key:this class is not key value coding compliant for the key is reported
了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
Ebpf cilium practice (2) - underlying network observability
Print stream and system setout();
QDataStream的簡單讀寫驗證
Lock free concurrency of JUC (leguan lock)
Set集合详细讲解
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Vmware workstation network card settings and three common network modes