awk工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了awk工具相关的知识,希望对你有一定的参考价值。

一、awk工具的使用
awk和sed一样是流式编辑器,它也是针对文档中的行来操作的。它比sed更强大。

1、截取文件中的某个段

head -2 /etc/passwd |awk -F ‘:‘ ‘{print $1}‘
[[email protected] ~]# head -2 /etc/passwd |awk -F ‘:‘ ‘{print $1}‘
root
bin
[[email protected] ~]# ^C

-F 指定分隔符,如果不加-F指定,则以空格或者tab为分隔符
$1 第1字段,$2第2字段,$0表示整行
print 打印,print的动作要用 { } 括起来,它还可以打印自定义的内容。但自定义的内容要用双引号引起来。例:

head -2 /etc/passwd |awk -F ‘:‘ ‘{print $1"#"$2"#"$3"#"$4}‘
[[email protected] ~]# head -2 /etc/passwd |awk -F ‘:‘ ‘{print $1"#"$2"#"$3"#"$4}‘
root#x#0#0
bin#x#1#1
[[email protected] ~]# 

2、匹配字符或字符串(跟sed用法差不多)

例:awk ‘/ro/‘ 1.txt 匹配ro字符
awk -F ‘:‘ ‘$! ~/oo/‘ 1.txt ~是匹配的意思

awk -F ‘:‘ ‘/root/ {print $1,$3} /test/ {print $1,$3}‘ /etc/passwd

[[email protected] ~]# awk -F ‘:‘ ‘/root/ {print $1,$3} /test/ {print $1,$3}‘ /etc/passwd
root 0
operator 11
test 1003
[[email protected] ~]# 

多次匹配,匹配完root,再匹配test,它还可以只打印所匹配的段

3、条件操作符

awk中是可以用逻辑符号判断的,比如 ‘==’ 就是等于,也可以理解为 ‘精确匹配’ 另外也有 >, ‘>=, ‘<, ‘<=, ‘!= 等等,
awk -F ‘:‘ ‘$3="0"‘ /etc/passwd

[[email protected] ~]# awk -F ‘:‘ ‘$3=="0"‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# 

在和数字比较时,若把比较的数字用双引号引起来后,那么awk会认为是字符,不加双引号则认为是数字。
例:
awk -F ‘:‘ ‘$3 >=500‘ /etc/passwd 打印UID大于或等于500的行

[[email protected] ~]# awk -F ‘:‘ ‘$3 >=500‘ /etc/passwd
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
riven:x:1000:1000::/home/riven:/bin/bash
test:x:1003:1001::/home/test:/bin/bash
user2:x:1004:1004::/home/user2:/bin/bash
httpd:x:1005:1012::/home/httpd:/bin/nologin
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[[email protected] ~]# awk -F ‘:‘ ‘$3 >="500"‘ /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[[email protected] ~]# 

!= 为不匹配,例:awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd 不匹配/sbin/nologin ,

[[email protected] ~]# awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
riven:x:1000:1000::/home/riven:/bin/bash
test:x:1003:1001::/home/test:/bin/bash
user2:x:1004:1004::/home/user2:/bin/bash
httpd:x:1005:1012::/home/httpd:/bin/nologin
[[email protected] ~]# 

除了针对某一个段的字符进行逻辑比较外,还可以两个段之间进行逻辑比较。
例:awk -F ‘:‘ ‘$3<$4‘ /etc/passwd 第三个字符小于第四个字符的项。

[[email protected] ~]# awk -F ‘:‘ ‘$3<$4‘ /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
httpd:x:1005:1012::/home/httpd:/bin/nologin
[[email protected] ~]# awk -F ‘:‘ ‘$4<$3‘ /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
test:x:1003:1001::/home/test:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[[email protected] ~]# 

&& 和 || 表示 “并且” 和 “或者” 的意思。

[[email protected] ~]# awk -F ‘:‘ ‘$3>"5" && $3<"7"‘ /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[[email protected] ~]# awk -F ‘:‘ ‘$3>"3" && $3<"7"‘ /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
ntp:x:38:38::/etc/ntp:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[[email protected] ~]# awk -F ‘:‘ ‘$3>"3" || $7<"/bin/bash"‘ /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
[[email protected] ~]#

4、awk的内置变量

awk常用的变量有:

NF :用分隔符分隔后一共有多少段

NR :行数

[[email protected] ~]#  head -2 /etc/passwd | awk -F ‘:‘ ‘{print NF}‘
7
7
[[email protected] ~]#  head -2 /etc/passwd | awk -F ‘:‘ ‘{print $NF}‘
/bin/bash
/sbin/nologin
[[email protected] ~]#  head -2 /etc/passwd | awk -F ‘:‘ ‘{print $NR}‘
root
x
[[email protected] ~]#  head -2 /etc/passwd | awk -F ‘:‘ ‘{print NR}‘
1
2
[[email protected] ~]# 

NF 是多少段,而$NF是最后一段的值, 而NR则是行号。$NR对应的是:行号是几显示的就是第几个段的字符。

5、awk中的数学运算

awk可以把段值更改:
例: head -n 3 /etc/passwd |awk -F ‘:‘ ‘$1="root"‘

[[email protected] ~]#  head -n 3 /etc/passwd |awk -F ‘:‘ ‘$1="root"‘
root x 0 0 root /root /bin/bash
root x 1 1 bin /bin /sbin/nologin
root x 2 2 daemon /sbin /sbin/nologin

对各个段的值进行数学运算:


[[email protected] ~]# head -n2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[[email protected] ~]# head -n2 /etc/passwd |awk -F ‘:‘ ‘{$7=$3+$4}‘
[[email protected] ~]# head -n2 /etc/passwd |awk -F ‘:‘ ‘{$7=$3+$4; print $0}‘
root x 0 0 root /root 0
bin x 1 1 bin /bin 2
[[email protected] ~]# 

可以计算某个段的总和:


[[email protected] ~]#  awk -F ‘:‘ ‘{(tot=tot+$3)}; END {print tot}‘ /etc/passwd  #END 表示所有的行都已经执行
7800
[[email protected] ~]# 

在awk中使用if判断、for循环:

[[email protected] ~]# awk -F ‘:‘ ‘{if ($1=="root") print $0}‘ /etc/passwd
root:x:0:0:root:/root:/bin/bash
[[email protected] ~]# 

以上是关于awk工具的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

Word 文档的优秀代码片段工具或插件?

linux awk命令的使用

前端开发工具vscode如何快速生成代码片段

前端开发工具vscode如何快速生成代码片段

elasticsearch代码片段,及工具类SearchEsUtil.java