当前位置:网站首页>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 CAD 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
- 液压滑环的特点讲解
- QDataStream的簡單讀寫驗證
- 複制寶貝提示材質不能為空,如何解决?
- Youqitong [vip] v3.7.2022.0106 official January 22 Edition
- 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
- TypeORM 框架
- 导电滑环短路的原因以及应对措施
- [NLP Li Hongyi] notes
猜你喜欢

Causes of short circuit of conductive slip ring and Countermeasures

数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化

Manually implement a simple stack

工业导电滑环的应用

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

Actual combat: basic use of Redux

使用 Nocalhost 开发 Rainbond 上的微服务应用

Daily question -leetcode1175- permutation of prime numbers - Mathematics

实战:redux的基本使用

Dynamic verification of new form items in El form; El form verifies that the dynamic form V-IF does not take effect;
随机推荐
Sqlplus connects using the instance name
积分商城游戏能够给商家带来什么?怎么搭建积分商城?
Some common commands of podman
Thread process foundation of JUC
JS random verification code
LevelDB源码分析之LRU Cache
Causes of short circuit of conductive slip ring and Countermeasures
Data consistency between redis and database
Lock free concurrency of JUC (leguan lock)
了解 JVM 中几个相关问题 — JVM 内存布局、类加载机制、垃圾回收
HCIP Day13
Global and Chinese market of enterprise wireless LAN 2022-2028: Research Report on technology, participants, trends, market size and share
Unit testing with mongodb
Worried about infringement? Must share copyrightless materials on the website. Don't worry about the lack of materials for video clips
AcWing 889. 01 sequence satisfying the condition (Cartland number)
Global and Chinese market of digital badge 2022-2028: Research Report on technology, participants, trends, market size and share
Series of improving enterprise product delivery efficiency (1) -- one click installation and upgrade of enterprise applications
Global and Chinese market for instant messaging security and compliance solutions 2022-2028: Research Report on technology, participants, trends, market size and share
Application of industrial conductive slip ring
More than one file was found with OS independent path ‘lib/armeabi-v7a/libyuv. so‘.