17 文本处理三剑客之AWK

[root@CentOS8 data]#awk -F: 'BEGIN{print "|----------USER-----------|----UID----|--------SHELL--------|"}{printf "|USER:%-20s|UID:%-7s|SHELL:%-15s|\n|-------------------------|-----------|---------------------|\n",$1,$3,$7}' /etc/passwd
|----------USER-----------|----UID----|--------SHELL--------|
|USER:root                |UID:0      |SHELL:/bin/bash      |
|-------------------------|-----------|---------------------|
|USER:bin                 |UID:1      |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:daemon              |UID:2      |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:adm                 |UID:3      |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:lp                  |UID:4      |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:sync                |UID:5      |SHELL:/bin/sync      |
|-------------------------|-----------|---------------------|
|USER:shutdown            |UID:6      |SHELL:/sbin/shutdown |
|-------------------------|-----------|---------------------|
|USER:halt                |UID:7      |SHELL:/sbin/halt     |
|-------------------------|-----------|---------------------|
|USER:mail                |UID:8      |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:operator            |UID:11     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:games               |UID:12     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:ftp                 |UID:14     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:nobody              |UID:65534  |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:dbus                |UID:81     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:systemd-coredump    |UID:999    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:systemd-resolve     |UID:193    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:tss                 |UID:59     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:polkitd             |UID:998    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:geoclue             |UID:997    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:rtkit               |UID:172    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:pulse               |UID:171    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:qemu                |UID:107    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:usbmuxd             |UID:113    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:unbound             |UID:996    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:rpc                 |UID:32     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:gluster             |UID:995    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:chrony              |UID:994    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:libstoragemgmt      |UID:993    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:saslauth            |UID:992    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:dnsmasq             |UID:986    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:radvd               |UID:75     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:clevis              |UID:985    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:cockpit-ws          |UID:984    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:colord              |UID:983    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:rpcuser             |UID:29     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:sssd                |UID:982    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:setroubleshoot      |UID:981    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:pipewire            |UID:980    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:gdm                 |UID:42     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:gnome-initial-setup |UID:979    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:insights            |UID:978    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:sshd                |UID:74     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:avahi               |UID:70     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:tcpdump             |UID:72     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:yanyi               |UID:1000   |SHELL:/bin/bash      |
|-------------------------|-----------|---------------------|
|USER:apache              |UID:48     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:pesign              |UID:977    |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
|USER:ntp                 |UID:38     |SHELL:/sbin/nologin  |
|-------------------------|-----------|---------------------|
[root@CentOS8 data]#df|awk -F" +|%" 'BEGIN{print "|------dev------|-usr-|"}{printf  "|%-15s|%5d|\n|---------------|-----|\n",$1,$5}'
|------dev------|-usr-|
|Filesystem     |    0|
|---------------|-----|
|devtmpfs       |    0|
|---------------|-----|
|tmpfs          |    0|
|---------------|-----|
|tmpfs          |    2|
|---------------|-----|
|tmpfs          |    0|
|---------------|-----|
|/dev/nvme0n1p2 |    5|
|---------------|-----|
|/dev/nvme0n1p5 |    1|
|---------------|-----|
|/dev/nvme0n1p1 |   15|
|---------------|-----|
|tmpfs          |    1|
|---------------|-----|
|tmpfs          |    1|
|---------------|-----|

[root@CentOS8 data]#df|awk 'NR!=1{print $1,$5}'
devtmpfs 0%
tmpfs 0%
tmpfs 2%
tmpfs 0%
/dev/nvme0n1p2 5%
/dev/nvme0n1p5 1%
/dev/nvme0n1p1 15%
tmpfs 1%
tmpfs 1%
[root@CentOS8 data]#awk  -F'[.|/]' '{print $3}' test2.txt 
www
mp3
map

[root@CentOS8 data]#cat test2.txt 
http://www.baidu.com
http://mp3.baidu.com/index.html
http://map.baidu.com/
[root@CentOS8 data]#awk " " /etc/issue
[root@CentOS8 data]#awk '" "' /etc/issue
\S
Kernel \r on an \m

[root@CentOS8 data]#awk " " /etc/issue
[root@CentOS8 data]#awk '" "' /etc/issue
\S
Kernel \r on an \m

[root@CentOS8 data]#awk "1" /etc/issue
\S
Kernel \r on an \m

[root@CentOS8 data]#awk "0" /etc/issue
[root@CentOS8 data]#awk "2" /etc/issue
\S
Kernel \r on an \m
[root@CentOS8 data]#awk -v x=10 'x' /etc/issue
\S
Kernel \r on an \m

[root@CentOS8 data]#awk -v x=0 'x' /etc/issue
[root@CentOS8 data]#awk -v x=0 '!x' /etc/issue
\S
Kernel \r on an \m

[root@CentOS8 data]#awk -v x=" " 'x' /etc/issue
\S
Kernel \r on an \m
[root@CentOS8 data]#seq 10 |awk 'x=!x{print x,$0}'
1 1
1 3
1 5
1 7
1 9
[root@CentOS8 data]#seq 10 |awk 'x=!x{print x}'
1
1
1
1
1
[root@CentOS8 data]#seq 10 |awk '{x=!x;print x}'
1
0
1
0
1
0
1
0
1
0
[root@CentOS8 data]#seq 10 |awk '{x=!x;print x,$0}'
1 1
0 2
1 3
0 4
1 5
0 6
1 7
0 8
1 9
0 10
[root@CentOS8 data]#seq 10 |awk 'x=!x{print x,$0}'
1 1
1 3
1 5
1 7
1 9
[root@CentOS8 data]#seq 10 |awk -v x=1 'x=!x{print x,$0}'
1 2
1 4
1 6
1 8
1 10

[root@CentOS8 data]#seq 10|sed -n '1~2P'
1
3
5
7
9
[root@CentOS8 data]#seq 10|sed -n '2~2P'
2
4
6
8
10
[root@CentOS8 data]#seq 10|sed -n '2~3P'
2
5
8
[root@CentOS8 data]#seq 10|sed -n '1~3P'
1
4
7
10
[root@CentOS8 data]#seq 10|sed -n '1~4P'
1
5
9
[root@CentOS8 data]#seq 10|sed -n '2~4P'
2
6
10
```bash
[root@CentOS8 data]#awk -F: '/^root/,/^nobody/{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
[root@CentOS8 data]#awk -F: '/^root\>/,/^nobody\>/{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody

[root@CentOS8 data]#awk -F: 'NR>=10&&NR<=20{print NR,$1}' /etc/passwd
10 operator
11 games
12 ftp
13 nobody
14 dbus
15 systemd-coredump
16 systemd-resolve
17 tss
18 polkitd
19 geoclue
20 rtkit
[root@CentOS8 data]#awk -F: '(NR>=10&&NR<=20){print NR,$1}' /etc/passwd
10 operator
11 games
12 ftp
13 nobody
14 dbus
15 systemd-coredump
16 systemd-resolve
17 tss
18 polkitd
19 geoclue
20 rtkit
[root@CentOS8 data]#df -h |awk -F" +|%" 'NR!=1{if($5>=10){print $1,"will be full",$5}else if($5>=4){print $1,"is used well",$5}else{print $1,"is ok",$5}}'
devtmpfs is ok 0
tmpfs is ok 0
tmpfs is ok 2
tmpfs is ok 0
/dev/nvme0n1p2 is used well 5
/dev/nvme0n1p5 is ok 1
/dev/nvme0n1p1 will be full 15
tmpfs is ok 1
tmpfs is ok 1
[root@centos7 ~]#awk '/^[[:space:]]*linux16/{i=1;while(i<=NF){print $i,length($i),NF; i++}}' /etc/grub2.cfg
linux16 7 8
/vmlinuz-3.10.0-1062.el7.x86_64 31 8
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46 8
ro 2 8
crashkernel=auto 16 8
rhgb 4 8
quiet 5 8
net.ifnames=0 13 8
linux16 7 9
/vmlinuz-3.10.0-1062.el7.x86_64 31 9
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46 9
ro 2 9
single 6 9
crashkernel=auto 16 9
rhgb 4 9
quiet 5 9
net.ifnames=0 13 9
linux16 7 8
/vmlinuz-0-rescue-f73e29bae4f54acba607d5eb2d707a4d 50 8
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46 8
ro 2 8
crashkernel=auto 16 8
rhgb 4 8
quiet 5 8
net.ifnames=0 13 8
linux16 7 9
/vmlinuz-0-rescue-f73e29bae4f54acba607d5eb2d707a4d 50 9
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46 9
ro 2 9
single 6 9
crashkernel=auto 16 9
rhgb 4 9
quiet 5 9
net.ifnames=0 13 9


[root@centos7 ~]#awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}'  /etc/grub2.cfg
linux16 7
/vmlinuz-3.10.0-1062.el7.x86_64 31
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46
ro 2
crashkernel=auto 16
rhgb 4
quiet 5
net.ifnames=0 13
linux16 7
/vmlinuz-3.10.0-1062.el7.x86_64 31
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46
ro 2
single 6
crashkernel=auto 16
rhgb 4
quiet 5
net.ifnames=0 13
linux16 7
/vmlinuz-0-rescue-f73e29bae4f54acba607d5eb2d707a4d 50
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46
ro 2
crashkernel=auto 16
rhgb 4
quiet 5
net.ifnames=0 13
linux16 7
/vmlinuz-0-rescue-f73e29bae4f54acba607d5eb2d707a4d 50
root=UUID=68a1ce5d-c938-4b66-8e6f-cb36e665087c 46
ro 2
single 6
crashkernel=auto 16
rhgb 4
quiet 5
net.ifnames=0 13
[root@centos7 ~]#awk 'BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++){sum=j*i;printf "%-s*%s=%s\t",j,i,sum}printf "\n"}}'
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
[root@centos7 ~]#awk 'BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++){sum=j*i;printf "%-sx%s=%s\t",j,i,sum}printf "\n"}}'
1x1=1	
1x2=2	2x2=4	
1x3=3	2x3=6	3x3=9	
1x4=4	2x4=8	3x4=12	4x4=16	
1x5=5	2x5=10	3x5=15	4x5=20	5x5=25	
1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	
1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	
1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	
1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	
[root@centos7 ~]#awk '!line[$0]++{print $0, line[$0]}' a.txt 
a 1
b 1
cc 1
c 1
ccc 1
dd 1
s 1
ds 1
d 1
[root@centos7 ~]#awk '!line[$0]++{print $0, !line[$0]}' a.txt 
a 0
b 0
cc 0
c 0
ccc 0
dd 0
s 0
ds 0
d 0
[root@centos7 ~]#awk '!line[$0]++{print $0, line[$0]}' a.txt 
a 1
b 1
cc 1
c 1
ccc 1
dd 1
s 1
ds 1
d 1
[root@centos7 ~]#awk 'line[$0]++{print $0, line[$0]}' a.txt 
a 2
b 2
cc 2
s 2
d 2
[root@centos7 ~]#cat a.txt
a
a
b
b
cc
c
ccc
cc
dd
s
ds
d
s
d
将以下文本以inode为标记,对inode相同的counts进行累加,并且统计出同一inode中, beginnumber的最小值和
endnumber的最大值
[root@centos7 data]#cat inode.txt 
inode|beginnumber|endnumber|counts|
106|3363120000|3363129999|10000|
106|3368560000|3368579999|20000|
310|3337000000|3337000100|101|
310|3342950000|3342959999|10000|
310|3362120960|3362120961|2|
311|3313460102|3313469999|9898|
311|3313470000|3313499999|30000|
311|3362120962|3362120963|2|

[root@centos7 data]#awk -F"|" -v OFS="|" 'NR!=1{counts[$1]+=$4;inode[$1]++;if(!bn[$1]){bn[$1]=$2}else if(bn[$1]>$2){bn[$1]=$2};if(!en[$1]){en[$1]=$3}else if(en[$1]<$3){en[$1]=$3}}END{for(i in inode)print i,bn[i],en[i],counts[i]}' inode.txt 
310|3337000000|3362120961|10103
311|3313460102|3362120963|39900
106|3363120000|3368579999|30000


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值