Linux three swordsmen awk
awk description
awk - pattern scanning and processing languageļ¼
awk is gawk, which is more like a language and is very powerful in file processing;
awk can customize variables, functions, loops, conditional statements, etc;
awk judges and processes data line by line;
awk usage
awk command syntax
Preceding command | awk [option] '[condition] {instruction}'
awk [option] '[condition] {instruction}' file
awk common options
option | describe |
---|---|
-F | Field separator, which defaults to space or tab |
-v | Define variables |
-f <scriptfile> | Reads the awk command from the specified script file |
awk built in variable
Variable name | meaning |
---|---|
FS=':' | Set the field separator, the same as the - F option |
$n | Specifies the nth field of the separator |
$0 | Read the entire line |
NF | Number of fields (columns) after separator |
NR | Number of rows read in |
awk basic usage
- Print the contents of the specified column
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# lsb_release -a | grep Distributor Distributor ID: CentOS [root@centos-36_2 httpd]# lsb_release -a | grep Distributor | awk '{print $3}' CentOS [root@centos-36_2 httpd]#
- Print the contents of the specified column after filtering (the row number is used to judge the filtering in the following example)
[root@centos-36_2 httpd]# free -h label total used free shared buff/cache available Mem: 7.4G 1.5G 4.4G 124M 1.5G 5.3G Swap: 7.6G 0B 7.6G [root@centos-36_2 httpd]# free -h | awk 'NR>1{print $4}' 4.4G 7.6G [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# free -h | awk 'NR==2{print "Mem free: "$4}NR==3{print "Swap free: "$4}' Mem free: 4.4G Swap free: 7.6G [root@centos-36_2 httpd]#
awk field separation
- For example, the default separation is by space or tab
[root@centos-36_2 httpd]# cat /etc/passwd | grep bash root:x:0:0:root:/root:/bin/bash amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash admin:x:1000:1000:admin:/home/admin:/bin/bash vserver:x:1018:1018::/home/vserver:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash user05:x:1027:1029::/home/user05:/bin/bash python:x:1028:1030::/home/python:/bin/bash git:x:1029:1031::/home/git:/bin/bash [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# cat /etc/passwd | grep bash | awk '{print $1}' root:x:0:0:root:/root:/bin/bash amandabackup:x:33:6:Amanda admin:x:1000:1000:admin:/home/admin:/bin/bash vserver:x:1018:1018::/home/vserver:/bin/bash mysql:x:27:27:MySQL user05:x:1027:1029::/home/user05:/bin/bash python:x:1028:1030::/home/python:/bin/bash git:x:1029:1031::/home/git:/bin/bash [root@centos-36_2 httpd]#
- Use the - F option to specify a separator example
[root@centos-36_2 httpd]# cat /etc/passwd | grep bash root:x:0:0:root:/root:/bin/bash amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash admin:x:1000:1000:admin:/home/admin:/bin/bash vserver:x:1018:1018::/home/vserver:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash user05:x:1027:1029::/home/user05:/bin/bash python:x:1028:1030::/home/python:/bin/bash git:x:1029:1031::/home/git:/bin/bash [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# cat /etc/passwd | grep bash | awk -F: '{print $1}' root amandabackup admin vserver mysql user05 python git [root@centos-36_2 httpd]#
- Examples of specifying delimiters using FS built-in variables
[root@centos-36_2 httpd]# cat /etc/passwd | grep bash root:x:0:0:root:/root:/bin/bash amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash admin:x:1000:1000:admin:/home/admin:/bin/bash vserver:x:1018:1018::/home/vserver:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash user05:x:1027:1029::/home/user05:/bin/bash python:x:1028:1030::/home/python:/bin/bash git:x:1029:1031::/home/git:/bin/bash [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# cat /etc/passwd | grep bash | awk '{FS=":";print $1}' root:x:0:0:root:/root:/bin/bash amandabackup admin vserver mysql user05 python git [root@centos-36_2 httpd]#
- Specify multiple separator examples
[root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F- '{print $1}' ERROR [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F- '{print $2}' Get pktmap failed,pktId=7585 [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F- '{print $3}' [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F, '{print $1}' ERROR-Get pktmap failed [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F, '{print $2}' pktId=7585 [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F, '{print $3}' [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[,-] '{print $1}' ERROR [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[,-] '{print $2}' Get pktmap failed [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[,-] '{print $3}' pktId=7585 [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[,-] '{print $4}' [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $0}' ERROR-Get pktmap failed,pktId=7585 [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $1}' ERROR [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $2}' Get pktmap failed [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $3}' pktId [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $4}' 7585 [root@centos-36_2 httpd]# echo "ERROR-Get pktmap failed,pktId=7585" | awk -F[-=,] '{print $5}' [root@centos-36_2 httpd]#
awk built in variable NF/NR
- Print line N
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# lsb_release -a | awk 'NR==2{print}' Distributor ID: CentOS [root@centos-36_2 httpd]#
- Print even lines
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# lsb_release -a | awk 'NR%2==0{print}' Distributor ID: CentOS Release: 7.6.1810 [root@centos-36_2 httpd]#
- Print last column
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# lsb_release -a | awk 'NR==2{print}' Distributor ID: CentOS [root@centos-36_2 httpd]# lsb_release -a | awk 'NR==2{print $NF}' CentOS [root@centos-36_2 httpd]#
- Print the penultimate column
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# lsb_release -a | awk '/Description/{print $(NF-1)}' 7.6.1810 [root@centos-36_2 httpd]#
awk passing external variables
- The use of external variables in awk needs to be defined in advance through the - v option
[root@centos-36_2 httpd]# my_para="hahaha" [root@centos-36_2 httpd]# echo $my_para hahaha [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk 'BEGIN{print "para is: ",my_para}' para is: [root@centos-36_2 httpd]# awk 'BEGIN{print "para is: ",$my_para}' para is: [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v awk_para=my_para 'BEGIN{print "para is: ",awk_para}' para is: my_para [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v awk_para=$my_para 'BEGIN{print "para is: ",awk_para}' para is: hahaha [root@centos-36_2 httpd]#
awk workflow
-
Command syntax
Preceding command | awk [option]'BEGIN {instruction 1} [condition] {instruction 2} END {instruction 3} '
awk [options] 'BEGIN {instruction 1} [condition] {instruction 2} END {instruction 3}' fileBEGIN {instruction 1} process BEGIN {instruction 1} before all lines for initialization operation;
When [condition] {instruction 2} processes the content line by line, execute [condition] {instruction 2} for text editing;
END {instruction 3} processes END {instruction 3} after all lines to output processing results;
Workflow can be used alone or combined at will; -
Basic examples
[root@centos-36_2 httpd]# uname -a | awk 'BEGIN{print "print kernel"} {print } END{print "print complete"}' Print kernel 3.10.0-957.21.3.el7.x86_64 printing complete [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# tail -n 10 /etc/passwd | awk -F: 'BEGIN{print "USERNAME","UID"} {print $1,$3;i++} END{print "NUM:",i}' | column -t USERNAME UID tcpdump 72 unbound 990 vserver 1018 apache 48 mysql 27 gluster 988 saned 987 user05 1027 python 1028 git 1029 NUM: 10 [root@centos-36_2 httpd]#
- Count the number of users using bash interpreter
[root@centos-36_2 httpd]# cat /etc/passwd | awk '/bash$/{print}' root:x:0:0:root:/root:/bin/bash amandabackup:x:33:6:Amanda user:/var/lib/amanda:/bin/bash admin:x:1000:1000:admin:/home/admin:/bin/bash vserver:x:1018:1018::/home/vserver:/bin/bash mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash user05:x:1027:1029::/home/user05:/bin/bash python:x:1028:1030::/home/python:/bin/bash git:x:1029:1031::/home/git:/bin/bash [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# Cat / etc / passwd | awk 'begin {x = 0} / bash $/ {x + +} end {print "the number of bash interpreter users is:" x}' bash The number of interpreter users is:8 [root@centos-36_2 httpd]#
awk condition judgment method
awk condition judgment methods are mainly divided into regular matching and numerical character comparison
Use logical or, logical and when combining multiple conditions
Regular matching contains two formats: 1. / regular expression / 2. str_x ~ / regular expression / or str_x !~ / Regular expression/
Numeric character comparison characters include: < = > > =! ===
Logical operators include: & &||
- Regular matching example 1 (filtering users using bash interpreter)
[root@centos-36_2 httpd]# cat /etc/passwd | awk -F: '/bash$/{print $1}' root amandabackup admin vserver mysql user05 python git [root@centos-36_2 httpd]#
- Regular matching example 2 (filtering users with and without bash interpreter)
[root@centos-36_2 httpd]# cat /etc/passwd | awk -F: '$NF~"bash$"{print $1}' root amandabackup admin vserver mysql user05 python git [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# cat /etc/passwd | awk -F: '$NF!~"bash$"{print $1}' bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-network [root@centos-36_2 httpd]#
- Numerical comparison example (filtering users with UID greater than 1000)
[root@centos-36_2 httpd]# cat /etc/passwd | awk -F: '$3>1000{print "username:"$1 " UID:"$3}' username:nfsnobody UID:65534 username:vserver UID:1018 username:user05 UID:1027 username:python UID:1028 username:git UID:1029 [root@centos-36_2 httpd]#
- Example of character comparison (filter the line whose first column is Release)
[root@centos-36_2 httpd]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core [root@centos-36_2 httpd]# lsb_release -a | awk -F: '$1=="Release"{print}' Release: 7.6.1810 [root@centos-36_2 httpd]#
- Examples of combining multiple matching patterns (filtering users with UID greater than 1000 and using bash interpreter by default)
[root@centos-36_2 httpd]# Awk - F: '$NF ~ / bash $/ & & $3 > 1000 {print "user name: $1}' /etc/passwd user name: vserver user name: user05 user name: python user name: git [root@centos-36_2 httpd]#
- Example of combination of condition judgment and workflow (calculate the sum of numbers within 100 that can be divided by both 3 and 7)
[root@centos-36_2 httpd]# Seq 100 | awk 'begin {i = 1; sum = 0} $0% 3 = = 0 & & $0% 7 = = 0 {print "the number of" i "is:" $0; sum + = $0; i + +} end {print "sum is:" sum}' The first number is:21 The second number is:42 The third number is:63 The fourth number is:84 Sum is: 210 [root@centos-36_2 httpd]#
Use of awk if statement
-
Command syntax
Preceding command | awk [option] '{if (judgment) {instruction 1} else if() {instruction 2} else {instruction 3}}'
awk [options] '{if (judgment) {instruction 1} else if() {instruction 2} else {instruction 3}} fileAll if statements are in {};
-
Basic example (printing information when memory is less than 5G)
[root@centos-36_2 httpd]# free total used free shared buff/cache available Mem: 7758324 1614908 4597768 127708 1545648 5578416 Swap: 7995388 0 7995388 [root@centos-36_2 httpd]# Free | awk '/ MEM / {if ($4 < 5000000) {print "memory remaining" $4, "less than 5G"}}' Memory remaining 4597504 less than 5 G [root@centos-36_2 httpd]#
- Comprehensive examples (count the number of files of each type in / etc directory)
[root@centos-36_2 httpd]# ll /etc/|wc -l 315 [root@centos-36_2 httpd]# ll /etc | > awk 'BEGIN{f_num=0;d_num=0;o_num=0} > {if($1~/^-/) {f_num++} > else if($1~/^d/) {d_num++} > else {o_num++}} > END{print "The number of files is: "f_num,"The number of folders is: "d_num,"The number of other types is:"o_num} ' The number of files is: 143 The number of folders is: 156 The number of other types is:16 [root@centos-36_2 httpd]#
Use of awk for statement
-
Command syntax
Preceding command | awk [option] '{for (variable; condition; expression) {instruction}}'
awk [options] '{for (variable; condition; expression) {instruction}}' fileAll for statements are in {};
-
Basic example 1 (print even numbers less than 10)
[root@centos-36_2 httpd]# awk 'BEGIN{for(i=0;i<10;i+=2){print i}}' 0 2 4 6 8 [root@centos-36_2 httpd]#
- Basic example 2 (calculate the sum of 1-100)
[root@centos-36_2 httpd]# awk 'BEGIN{ > total=0; > for(i=0;i<=100;i++){total+=i} > print total}' 5050 [root@centos-36_2 httpd]#
Use of awk while statement
-
Command syntax
Preceding command | awk [options] '{while (expression) {instruction}}'
awk [options] '{while (expression) {directive}}' fileAll while statements are in {};
-
Basic example 1 (print even numbers less than 10)
[root@centos-36_2 httpd]# awk 'BEGIN{i=0;while(i<10){print i;i+=2}}' 0 2 4 6 8 [root@centos-36_2 httpd]#
- Basic example 2 (calculate the sum of 1-100)
[root@centos-36_2 httpd]# awk 'BEGIN{ > i=0; > total=0; > while(i<=100){total+=i;i++}; > print total; > }' 5050 [root@centos-36_2 httpd]#
Use of awk array
Using arrays in awk makes it easier to store and query information;
- Basic examples
[root@centos-36_2 httpd]# awk 'BEGIN{age["jack"]=18;age["tom"]=20;for(name in age){print name,"age is:",age[name]}}' jack age is: 18 tom age is: 20 [root@centos-36_2 httpd]#
- Comprehensive examples (Statistics of website access IP and times)
[root@centos-36_2 httpd]# tail -n3 access_log 192.166.165.14 - - [16/Nov/2021:09:53:12 +0800] "GET /layui/css/modules/laydate/default/laydate.css?v=5.3.1 HTTP/1.1" 200 7365 "http://192.166.160.21:8089/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0" 192.166.165.14 - - [16/Nov/2021:09:53:12 +0800] "GET /layui/css/modules/layer/default/layer.css?v=3.5.1 HTTP/1.1" 200 14271 "http://192.166.160.21:8089/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0" 192.166.165.14 - - [16/Nov/2021:09:53:12 +0800] "GET /layui/css/modules/code.css?v=2 HTTP/1.1" 200 1319 "http://192.166.160.21:8089/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0" [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk '{IP_num[]++} END{for(ip in IP_num) {print ip, "number of accesses:", IP_num[ip]}}' access_log 192.166.160.21 The number of visits is: 9 192.166.165.13 The number of visits is: 142 192.166.165.14 The number of visits is: 14 192.166.165.90 The number of visits is: 64 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# cat access_log | cut -d " " -f1 | sort | uniq -c # Other methods 9 192.166.160.21 142 192.166.165.13 14 192.166.165.14 64 192.166.165.90 [root@centos-36_2 httpd]#
awk built-in function
There are many built-in functions (string function, arithmetic function, time function, etc.) in awk;
Common functions are as follows:
function | function |
---|---|
length | Calculates the length of the string |
index | Find string B in string A |
match | Regular matching finds string B in string A |
sub | Replace the first match in the string |
gsub | Replace all matches in the string |
substr | Intercept string according to index |
split | Separates strings into arrays according to regular |
int | Return integer part |
rand | Returns a decimal number between 0 and 1 |
srand | Sets the seed value of the rand function |
system | Execute the specified linux command and return the status code |
systime | Current timestamp |
mktime | Generates the specified timestamp |
strftime | Format timestamp output |
Examples of common functions are as follows:
- Basic example of using length
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print length(info)}' 19 [root@centos-36_2 httpd]#
- Basic example of using index (return index of matching position)
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print index(info,"test")}' 11 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print index(info,"est")}' 12 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print index(info,"aest")}' 0 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print index(info,"test")?"OK":"not found"}' OK [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print index(info,"atest")?"OK":"not found"}' not found [root@centos-36_2 httpd]#
- match uses basic examples (more flexible than index)
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/[0-9]{4}/)}' 16 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/[0-9]{5}/)}' 0 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/\sis/)}' 5 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/\wis/)}' 2 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/[0-9]{5}/)?"ok":"no found";}' no found [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print match(info,/[0-9]{4}/)?"ok":"no found";}' ok [root@centos-36_2 httpd]#
- Basic examples of sub usage
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{sub(/[0-9]{2}/,"aaaa",info); print info}' this is a test aaaa34 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{gsub(/[0-9]{2}/,"aaaa",info); print info}' this is a test aaaaaaaa [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{sub(/is/,"aaaa",info); print info}' thaaaa is a test 1234 [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{gsub(/is/,"aaaa",info); print info}' thaaaa aaaa a test 1234 [root@centos-36_2 httpd]#
- substr basic example
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print substr(info,9,6)}' a test [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{print substr(info,11,4)}' test [root@centos-36_2 httpd]#
- Basic examples of split usage
[root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{split(info,array_x," ");for(x in array_x){print x,array_x[x]}}' 4 test 5 1234 1 this 2 is 3 a [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk -v info="this is a test 1234" 'BEGIN{split(info,array_x,"[3a]");for(x in array_x){print x,array_x[x]}}' 1 this is 2 test 12 3 4 [root@centos-36_2 httpd]#
- Generating random values using arithmetic functions
[root@centos-36_2 httpd]# awk 'BEGIN{print rand()}' 0.237788 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk 'BEGIN{srand(1);print rand()}' 0.237788 [root@centos-36_2 httpd]# awk 'BEGIN{srand(1);print rand()}' 0.237788 [root@centos-36_2 httpd]# awk 'BEGIN{srand(1);print rand()}' 0.237788 [root@centos-36_2 httpd]# awk 'BEGIN{srand(2);print rand()}' 0.610198 [root@centos-36_2 httpd]# awk 'BEGIN{srand(2);print rand()}' 0.610198 [root@centos-36_2 httpd]# awk 'BEGIN{srand(2);print rand()}' 0.610198 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk 'BEGIN{srand();print rand()}' 0.537146 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print rand()}' 0.156769 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print rand()}' 0.640628 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 476 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 326 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 274 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 382 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 852 [root@centos-36_2 httpd]# awk 'BEGIN{srand();print int(1000*rand())}' 731 [root@centos-36_2 httpd]#
- Call external commands
[root@centos-36_2 httpd]# awk 'BEGIN{t=system("date +%x");print}' 2021 November 19 [root@centos-36_2 httpd]# awk 'BEGIN{t=system("date +%x");print t}' 2021 November 19 0 [root@centos-36_2 httpd]#
- Print current timestamp
[root@centos-36_2 httpd]# awk 'BEGIN {print systime()}' 1637326861 [root@centos-36_2 httpd]# awk 'BEGIN {print systime()}' 1637326862 [root@centos-36_2 httpd]# awk 'BEGIN {print systime()}' 1637326863 [root@centos-36_2 httpd]# awk 'BEGIN {print systime()}' 1637326864 [root@centos-36_2 httpd]#
- Print the specified timestamp
[root@centos-36_2 httpd]# awk 'BEGIN{tstamp=mktime("2020 11 11 11 01 01");print tstamp}' 1605063661 [root@centos-36_2 httpd]# awk 'BEGIN{tstamp=mktime("2020 11 11 10 01 01");print tstamp}' 1605060061 [root@centos-36_2 httpd]#
- Timestamp formatted output
[root@centos-36_2 httpd]# awk 'BEGIN{tstamp=mktime("2020 11 11 11 01 01");print tstamp}' 1605063661 [root@centos-36_2 httpd]# awk 'BEGIN{print strftime("%c",1605060061)}' 2020 Wednesday, November 11, 2010 10:01:01 [root@centos-36_2 httpd]# [root@centos-36_2 httpd]# awk 'BEGIN{print strftime("%c",systime())}' 2021 Friday, November 19, 2013 21:03:31 [root@centos-36_2 httpd]#
awk summary
So far, the common functions of awk have been basically summarized. It can be seen that the function of awk is very powerful;
If you want to write shell scripts skillfully, it is essential to master the use of awk tools;
Tools still need more practice and use to play its powerful role;