Linux三剑客之-awk

Linux三剑客之-awk

awk 描述

awk - pattern scanning and processing language;
awk 即gawk,其更像是一门语言,对文件的处理十分强大;
awk 可以自定义变量,函数,循环,条件语句等;
awk 对数据的处理是一行一行的进行判断和处理;

awk 使用方式

awk 命令语法

前置命令 | awk [选项] '[条件]{指令}'
awk [选项] '[条件]{指令}' 文件

awk 常用选项
选项描述
-F字段分隔符,默认为空格或tab键
-v定义变量
-f <scriptfile>从指定脚本文件中读取awk命令
awk 内置变量
变量名含义
FS=’:’设置字段分隔符,同-F选项
$n指定分隔符的第N个字段
$0读入的整行内容
NF分隔符分隔后的字段数(列数)
NR读入的行数
awk 基本使用
  • 打印指定列内容
[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]# 
  • 过滤后打印指定列内容(下方举例中使用行号判断过滤)
[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 字段分隔
  • 默认分隔举例,默认按空格或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]# 
  • 使用-F选项指定分隔符举例
[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]# 
  • 使用FS内置变量指定分隔符举例
[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]# 
  • 指定多个分隔符举例
[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 内置变量NF/NR
  • 打印第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]# 
  • 打印第偶数行
[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]# 
  • 打印最后一列
[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]# 
  • 打印倒数第二列
[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 传递外部变量
  • awk中使用外部变量需要通过-v选项提前定义
[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 工作流程
  • 命令语法
    前置命令 | awk [选项] 'BEGIN{指令1} [条件]{指令2} END{指令3}'
    awk [选项] 'BEGIN{指令1} [条件]{指令2} END{指令3}' 文件

    BEGIN{指令1}在所有行前处理BEGIN{指令1},用于初始化操作;
    [条件]{指令2}逐行处理内容时执行[条件]{指令2},用于编辑文本操作;
    END{指令3}在所有行后处理END{指令3},用于输出处理结果;
    工作流可以单独使用也可以随意组合使用;

  • 基本举例

[root@centos-36_2 httpd]# uname -a | awk 'BEGIN{print "打印内核"} {print $3} END{print "打印完成"}' 
打印内核
3.10.0-957.21.3.el7.x86_64
打印完成
[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]# 
  • 统计使用bash解释器的用户数
[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 "bash解释器使用者数量为:"x}'
bash解释器使用者数量为:8
[root@centos-36_2 httpd]# 
awk 条件判断方式

awk条件判断方式主要分为正则匹配数值字符比较
多个条件组合时使用逻辑或逻辑与

正则匹配包含两种格式,1./正则表达式/ 2. str_x ~ /正则表达式/ or str_x !~ /正则表达式/
数值字符比较符包含:< <= > >= != ==
逻辑运算符包含:&& ||

  • 正则匹配举例1(过滤使用bash解释器的用户)
[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]# 
  • 正则匹配举例2(过滤使用和不使用bash解释器的用户)
[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]# 
  • 数值比较举例(过滤UID大于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]# 
  • 字符比较举例(过滤第一列为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]# 
  • 多个匹配模式结合比较举例(过滤UID大于1000且默认使用bash解释器的用户)
[root@centos-36_2 httpd]# awk -F: '$NF~/bash$/ && $3>1000 {print "用户名:"$1}' /etc/passwd
用户名:vserver
用户名:user05
用户名:python
用户名:git
[root@centos-36_2 httpd]# 
  • 条件判断与工作流结合举例(计算100以内既可以被3整除又可以被7整除的数的和)
[root@centos-36_2 httpd]# seq 100 | awk 'BEGIN{i=1;sum=0} $0%3==0 && $0%7==0{print "第"i"个数为:"$0;sum+=$0;i++} END{print "Sum is: "sum}'
第1个数为:21
第2个数为:42
第3个数为:63
第4个数为:84
Sum is: 210
[root@centos-36_2 httpd]# 
awk if语句的使用
  • 命令语法
    前置命令 | awk [选项] '{ if(判断){指令1} else if(){指令2} else{指令3} }'
    awk [选项] '{ if(判断){指令1} else if(){指令2} else{指令3} } 文件

    所有if语句均在{}中;

  • 基本举例(内存小于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 "内存剩余"$4,"小于5G"}}'
内存剩余4597504 小于5G
[root@centos-36_2 httpd]# 
  • 综合举例(统计/etc目录下各类型文件数)
[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 "文件数为: "f_num,"文件夹数为: "d_num,"其他类型数为:"o_num} '
文件数为: 143 文件夹数为: 156 其他类型数为:16
[root@centos-36_2 httpd]# 
awk for语句的使用
  • 命令语法
    前置命令 | awk [选项] '{ for(变量;条件;表达式){指令} }'
    awk [选项] '{ for(变量;条件;表达式){指令} }' 文件

    所有for语句均在{}中;

  • 基本举例1(打印小于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]#
  • 基本举例2(计算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]# 
awk while语句的使用
  • 命令语法
    前置命令 | awk [选项] '{ while(表达式){指令} }'
    awk [选项] '{ while(表达式){指令} }' 文件

    所有while语句均在{}中;

  • 基本举例1(打印小于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]# 
  • 基本举例2(计算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]# 
awk 数组的使用

awk中使用数组更容易进行信息存储和查询;

  • 基本举例
[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]# 
  • 综合举例(统计网站访问IP及次数)
[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[$1]++} END{for(ip in IP_num) {print ip,"访问次数为:",IP_num[ip]}}' access_log
192.166.160.21 访问次数为: 9
192.166.165.13 访问次数为: 142
192.166.165.14 访问次数为: 14
192.166.165.90 访问次数为: 64
[root@centos-36_2 httpd]# 
[root@centos-36_2 httpd]# cat access_log | cut -d " " -f1 | sort | uniq -c           # 其他方法
      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 内置函数

awk中存在较多内置函数(字符串函数、算术函数、时间函数等);
常用函数如下:

函数功能
length计算字符串的长度
index在字符串A中查找字符串B
match正则匹配方式在字符串A中查找字符串B
sub替换字符串中的第一个匹配项
gsub替换字符串中的所有匹配项
substr根据index截取字符串
split根据正则将字符串分隔为数组
int返整数部分
rand返回0-1之间的小数
srand设置rand函数的种子值
system执行指定的linux命令并返回状态码
systime当前时间时间戳
mktime生成指定时间时间戳
strftime将时间戳格式化输出

常用函数举例如下:

  • length使用基本举例
[root@centos-36_2 httpd]#  awk -v info="this is a test 1234" 'BEGIN{print length(info)}'
19
[root@centos-36_2 httpd]# 
  • index使用基本举例(返回匹配位置的index)
[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使用基本举例(比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]#
  • sub使用基本举例
[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使用基本举例
[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]# 
  • split使用基本举例
[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]# 
  • 利用算术函数生成随机值
[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]#  
  • 调用外部命令
[root@centos-36_2 httpd]# awk 'BEGIN{t=system("date +%x");print}'
2021年11月19日

[root@centos-36_2 httpd]# awk 'BEGIN{t=system("date +%x");print t}'
2021年11月19日
0
[root@centos-36_2 httpd]# 	
  • 打印当前时间戳
[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]#  	
  • 打印指定时间时间戳
[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]#  	
  • 时间戳格式化输出
[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年11月11日 星期三 10时01分01秒
[root@centos-36_2 httpd]#   
[root@centos-36_2 httpd]# awk 'BEGIN{print strftime("%c",systime())}'
2021年11月19日 星期五 21时03分31秒
[root@centos-36_2 httpd]# 

awk 总结

至此,awk常用功能已基本总结完毕,可以看出awk功能是非常强大的;
若想熟练编写shell脚本掌握awk工具的使用方法是必不可少的;
工具还是需要多加练习和使用才能发挥它的强大作用;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值