Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common uses of Awk

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Awk常用用法有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

awk

语法:

awk [option] 'pattern {action}' file

参数选项

-F 指定字段分隔符

-v 定义或者修改一个awk内部的变量

使用范例

[root@db1 ~]# cat -n passwd

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 operator:x:11:0:operator:/root:/sbin/nologin

显示文件第5行

root@db1 ~]# awk 'NR==5' passwd

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

显示文件2-6行

[root@db1 ~]# awk 'NR==2,NR==6' passwd

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

给文件显示行号

awk '{print NR,$0}' passwd

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 operator:x:11:0:operator:/root:/sbin/nologin

显示文件第2行到第6行,并显示行号

# awk 'NR==2,NR==6 {print NR, $0}' passwd

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

显示文件的第一列和最后一列

# awk -F ":" '{print $1,$NF}' passwd

root /bin/bash

bin /sbin/nologin

daemon /sbin/nologin

adm /sbin/nologin

lp /sbin/nologin

sync /bin/sync

shutdown /sbin/shutdown

halt /sbin/halt

mail /sbin/nologin

operator /sbin/nologin

用gsub函数把文件中的/sbin/nologin替换为/bin/bash

gsub("替换对象","替换成什么内容",哪一列)

# awk '{gsub("/sbin/nologin","/bin/bash",$0); print $0}' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/bin/bash

daemon:x:2:2:daemon:/sbin:/bin/bash

adm:x:3:4:adm:/var/adm:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/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

mail:x:8:12:mail:/var/spool/mail:/bin/bash

operator:x:11:0:operator:/root:/bin/bash

取出网卡的IP地址:

root@db1 ~]# ifconfig ens33

ens33: flags=4163 mtu 1500

inet 192.168.1.210 netmask 255.255.255.0 broadcast 192.168.1.255

inet6 fe80::20c:29ff:fe66:629a prefixlen 64 scopeid 0x20

ether 00:0c:29:66:62:9a txqueuelen 1000 (Ethernet)

RX packets 23839 bytes 2151619 (2.0 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 272 bytes 19255 (18.8 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

# ifconfig ens33|awk -F "(inet)|(netmask)" 'NR==2{print $2}'

192.168.1.210

说明:以(inet)|(netmask)为分隔符

# ifconfig ens33|awk -F " " 'NR==2{print $2}'

192.168.1.210

说明:以空格为分隔符

取出用户名

# awk -F ":" '{print $1}' passwd

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

operator

取出用户名后排序

# awk -F ":" '{print $1}' passwd|sort

abrt

adm

admin

apache

avahi

bin

chrony

cockpit-ws

colord

去重计数

awk -F ":" '{print $NF}' passwd|sort|uniq -c

# awk -F ":" '{print $NF}' passwd|sort|uniq -c

10 /bin/bash

1 /bin/sync

1 /sbin/halt

43 /sbin/nologin

1 /sbin/shutdown

第2种方法

创建数组

# awk -F ":" '{hotel[$NF]}' passwd

# awk -F ":" '{hotel[$NF];print $NF}' passwd

开始统计

# awk -F ":" '{hotel[$NF]++;print $NF,hotel[$NF]}' passwd

# awk -F ":" '{hotel[$NF]++}END{for(domain in hotel)print domain,hotel[domain]}' passwd

/bin/sync 1

/bin/bash 10

/sbin/nologin 43

/sbin/halt 1

/sbin/shutdown 1

行号小于3的行

# awk 'NR

< 3 {print $0}' passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin 以root开头的行 awk '/^root/{print $0}' passwd root:x:0:0:root:/root:/bin/bash 第3到第6行 # awk 'NR>

3 && NR

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report