当前位置:网站首页>Awk use
Awk use
2022-06-13 07:22:00 【Dongbei bird】
awk usage
Commands for text processing ; Easy to intercept content ; Support decimal operations
working principle ( And sed distinguish )
sed equally , Are read line by line 、 Handle ; sed Is a whole line of processing ,awk Is to split a row into fields for processing
Complete grammar
awk 'BEGIN{commands}pattern{commands}END{commands}'file BEGIN Execute the command before data
pattern Commands that are executed on each line
END Execute the command after the data
example : With : Separator , lookup passwd In file bash ending , Type the first field and the seventh field
[[email protected] 1-28]# cat passwd | awk -F: 'BEGIN{print "--start--"} /bash$/{print $1,$7}END{print "--end--"} '
--start--
root /bin/bash
yalin /bin/bash
califeng /bin/bash
cali123 /bin/bash
zhaoliying /bin/bash
woshi1 /bin/bash
woshi2 /bin/bash
--end--
Statistics passwd File lines
[[email protected] 1-28]# cat passwd | awk -F: 'BEGIN{i=0} {i++} END{print i}'
Basic usage
awk -F Separator ‘/ Pattern /{ action }’ file ; The default delimiter is blank ( Space or tab key )
awk Instructions must be enclosed in single quotation marks , Make sure you use curly braces {} Cover up
Patterns are regular expressions , Use / delimiter
Use... Between multiple actions ; Separate
Examples of basic commands
As long as the mode has no action result and grep similar , Show $0
[[email protected] 1-28]# awk '/bash/' passwd
root:x:0:0:root:/root:/bin/bash
yalin:x:1009:1009::/home/yalin:/bin/bash
califeng:x:1010:1010::/home/califeng:/bin/bash
cali123:x:1011:1011::/home/cali123:/bin/bash
zhaoliying:x:1012:1012::/home/zhaoliying:/bin/bash
woshi1:x:1013:1013::/home/woshi1:/bin/bash
woshi2:x:1014:1014::/home/woshi2:/bin/bash
Interception function
[[email protected] 1-28]# who | awk '{print $2}'
tty1
pts/0
Realize string splicing , The custom string needs to be ""
[[email protected] 1-28]# who | awk '{print "time:"$4,$2}'
time:10:29 tty1
time:10:29 pts/0
~ Fuzzy matching == Exactly match ! Mismatch
[[email protected] 1-28]# who | awk '$2~"tt" {print $0}'
root tty1 2021-01-28 10:29
[[email protected] 1-28]# who | awk '$2=="tty1" {print $0}'
root tty1 2021-01-28 10:29
[[email protected] 1-28]# who | awk '$2!="tty1" {print $0}'
root pts/0 2021-01-28 10:29 (192.168.0.123)
Used in conjunction with defining symbols \b Don't recognize Only use <>
[[email protected] 1-28]# cat passwd | awk -F: '$1 ~ /\<....\>/ {print $1}'
root
sync
halt
mail
dbus
sssd
sshd
rngd
awk Built-in variables
NR Represents the currently processed line number number of record
NF Number of fields representing a row number of field
FS Current input delimiter , The default is blank ( Space and tab)
FNR Number of records read from the file ( Line number ), from 1 Start
OFS Current output separator , Default is space
example : Output who The first, second, and last fields in , And use - Connect
[[email protected] 1-28]# who | awk 'OFS="-"{print $1,$2,$NF}'
root-tty1-10:29
root-pts/0-(192.168.0.123)
awk Built in functions
int(x) return x Truncated to the value of an integer
[[email protected] 1-9]# awk 'BEGIN {a=6.1572;result=int(a);print result}'
6
rand()
Return any number n(0<=n<1)
[[email protected] 1-9]# awk 'BEGIN {print rand()}'
0.924046
[[email protected]lhost 1-9]# awk 'BEGIN {print rand()}'
0.924046
No random seed is set , The random numbers are the same
srand()
Often with rand() Used together to produce random values
srand() It needs to be written in BEGIN modular , To generate random numbers normally , This is a awk The working mechanism of
[[email protected] 1-9]# awk 'BEGIN {srand();print rand()}'
0.501409
[[email protected] 1-9]# awk 'BEGIN {srand();print rand()}'
0.0852438
sub( Old string , New string , character string )
The third string is optional , The default is $0
[[email protected] 1-9]# awk 'BEGIN{ str="Hello World!";print "Before:" str;sub("World","Jack",str);print "After:" str }'
Before:Hello World!
After:Hello Jack!
index( character string 1, character string 2)
In string 1 Find string in 2, from 1 Numbered starting , Get a string 2 Value ; If not found, return 0
[[email protected] 1-9]# awk 'BEGIN{ str="AA BB CC";subs="BB";ret=index(str,sub);printf ret}'
4
ength( character string )
Returns the length of the string
[[email protected] 1-9]# awk 'BEGIN {str="hello!!I am Anna!";print length(str)}'
17
split( character string , Array , Separator )
Divide the string into array elements according to the separator and store them in the array
[[email protected] 1-9]# awk 'BEGIN {str="AA,BB,CC,DD,EE";split(str,arr,",");for(i in arr){ print arr[i]}}'
AA
BB
CC
DD
EE
system
Get the command return value ;0 Indicates that the last command was executed successfully ; Not 0 Indicates that the last command failed to execute
[[email protected] 1-9]# awk 'BEGIN { ret=system("date");print ret}'
2021 year 01 month 09 Japan Saturday 20:48:17 CST
0
awk quote shell Variable
-v quote shell Variable ( When there are fewer variables )
[[email protected] 1-9]# name="lisi"
[[email protected] 1-9]# echo | awk -v abc=$name '{print abc}'
lisi
Use double quotation marks, single quotation marks and take change The amount enclosed rise Come on ; along order yes : double lead Number single lead Number Enclose variables ; The order is : Double quotation marks, single quotation marks take change The amount enclosed rise Come on ; along order yes : double lead Number single lead Number Single quotation mark and double quotation mark
[[email protected] 1-9]# awk 'BEGIN{print "'$name'"}'
lisi
Use double quotes ,awk Inside 0 、 0、 0、n And so on
[[email protected] 1-9]# abc=zhaoliying
[[email protected] 1-9]# awk -F: "/^$abc/{print NR,\$0} " /etc/passwd
29 zhaoliying:x:1012:1012::/home/zhaoliying:/bin/bash
[[email protected] 1-9]# awk -F: "/^$abc/{print \$1,\$3} " /etc/passwd
zhaoliying 1012
Use single quotes , Splicing parameter transfer
sg=3
[[email protected] ~]# awk -F: '/root/{print $1,$'$sg'}' /etc/passwd
root 0
operator 11
-------------------------------------------------------------------
[[email protected] ~]#awk -F: "/root/{print \$1,\$sg}" /etc/passwd
root root:x:0:0:root:/root:/bin/bash
operator operator:x:11:0:operator:/root:/sbin/nologin
practice
View users who have not set a password ( The second field is * perhaps !! Not set )
cat shadow | awk -F: ‘BEGIN{i=0} {length($2)<=2;i++;print $1" No password set "} END{print “ Yes "i" No password is set ”}’
Use NF Variable display passwd The contents of the penultimate column of the document
cat passwd | awk -F’[: /]’ ‘{print $(NF-1)}’
Show passwd Document No. 5 To the first 10 The user name of the line
cat passwd | awk -F: ‘{print $1}’ | sed -n ‘5p;10p’
cat passwd | awk -F: ‘NR ~/<[5-9]|10>/ {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲’ Show passwd Line number in file …/ {print NR,KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲’ passwd use ip ad…NF ~/ens33/{print $2}’ | awk -F/ ‘{print $1}’
First use ifconfig, Use awk Show eth0 Inbound and outbound traffic ( byte )
ifconfig |head -8 | awk ‘$1 ~/RX/ && $4 ~ /bytes/ || $1 ~ /TX/ && $4 ~ /bytes/ {print $0}’
ifconfig | head -8 | awk ‘$0 ~ /RX.*bytes|TX.*bytes/ {print $5}’
Use awk Command statistics are based on r Number of users at the beginning , The results are as follows
cat passwd | awk -F: ‘BEGIN{i=0} $1 ~ /^r/ {print $1;i++} END {print i};’
Show passwd Document No. 7 Column is not bash Username
cat passwd | awk -F: ‘KaTeX parse error: Undefined control sequence: < at position 7: 7 !~ /\̲<̲bash>/{print $1}’
边栏推荐
- A. Vacations (DP greed
- [Markov chain Monte Carlo] Markov chain Monte Carlo method sampling prior distribution
- Tidb server tuning
- One article of quantitative framework backtrader read analyzer
- Wechat applet - positioning, map display, route planning and navigation
- First day of learning MySQL Basics
- 基于ESP32CAM实现WebSocket服务器实时点灯
- Problems encountered during commissioning of C # project
- redis-1. Install redis with pictures and texts
- [weak transient signal detection] matlab simulation of SVM detection method for weak transient signal under chaotic background
猜你喜欢

Tree list under winfrom treelist related

RT-Thread 模拟器 simulator LVGL控件:button 按钮事件
![[RS-422 and RS-485] RS-422 and RS-485 serial interface standard](/img/08/e50df07d387b2f2d57a518caedc795.jpg)
[RS-422 and RS-485] RS-422 and RS-485 serial interface standard

Uploading and retrieving stored images in localstorage

SDN基本概述

Un des backtraders du cadre de quantification lit l'analyseur

It's called the next generation monitoring system. Let's see how awesome it is

百货中心供应链管理系统

TiDB Lightning

First day of learning MySQL Basics
随机推荐
领先企业的管理实践证明,企业可持续发展的核心是什么?
Priority analysis of list variables in ansible playbook and how to separate and summarize list variables
Simple understanding of basic language of C language
One article of quantitative framework backtrader read analyzer
汇编语言基础:寄存器和寻址方式
P6154 游走(记忆化搜索
June 12, 2022: if there are n*n pieces in an n*n square chessboard, each grid can have exactly one piece. But now some pieces are gathered on a grid, such as 2030100300. The above two-dimensional arra
2022-06-12:在N*N的正方形棋盤中,有N*N個棋子,那麼每個格子正好可以擁有一個棋子。 但是現在有些棋子聚集到一個格子上了,比如: 2 0 3 0 1 0 3 0 0 如上的二維數組代錶,一
A solution to the problem that there is always a newline character when C merges multiple RichTextBox contents
Raspberry school advanced development - "writing of IO port driver code" includes bus address, physical \u virtual address and bcm2835 chip manual knowledge
mysql中时间字段 比较时间大小
JMeter encryption interface test
量化框架backtrader之一文读懂Analyzer分析器
C Advanced Programming - features
Real time lighting of websocket server based on esp32cam
Issues related to C # delegation and events
Local file upload FTP or remote directory
Table access among Oracle database users
Tikv key performance parameters and optimization
理財產品連續幾天收益都是零是怎麼回事?