awk

Posted 森森2017

tags:

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

awk

可以处理格式明显的文件,而sed不能

-F:代表以分号做分隔符,print $1,$4意思是打印第1个和第4个

[[email protected] ~]# awk -F: ‘{print $1,$4}‘ b.txt

root 0

bin 1

daemon 2

adm 4

lp  123()17   7

sync 0

shutdown 0

halt 0

mail 12

operator 0

[[email protected] ~]#

 

$0是取整行的意思

[[email protected] ~]# awk -F: ‘{print $0}‘ b.txt

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

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  123()17  :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

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

[[email protected] ~]#

 

NF表示有多少段

[[email protected] ~]# awk -F: ‘{print $1,NF}‘ b.txt

root 7

bin 7

daemon 7

adm 7

lp  123()17   7

sync 7

shutdown 7

halt 7

mail 7

operator 7

[[email protected] ~]#

 

由于我们不一定都知道最后一个是第几个,可以用NF代替

[[email protected] ~]# awk -F: ‘{print $1,$NF}‘ b.txt

root /bin/bash

bin /sbin/nologin

daemon /sbin/nologin

adm /sbin/nologin

lp  123()17   /sbin/nologin

sync /bin/sync

shutdown /sbin/shutdown

halt /sbin/halt

mail /sbin/nologin

operator /sbin/nologin

[[email protected] ~]#

 

NR是序号:

[[email protected] ~]# awk -F: ‘{print $1,NR}‘ b.txt

root 1

bin 2

daemon 3

adm 4

lp  123()17   5

sync 6

shutdown 7

halt 8

mail 9

operator 10

[[email protected] ~]#

 

只取第一行

[[email protected] ~]# awk -F: ‘NR==1{print $1,NR}‘ b.txt

root 1

[[email protected] ~]# awk -F: ‘NR<=3 {print $1,NR}‘ b.txt

root 1

bin 2

daemon 3

[[email protected] ~]#

 

取小于等于3行

[[email protected] ~]# awk -F: ‘NR<=3 {print NR,"------",$1}‘ b.txt

1 ------ root

2 ------ bin

3 ------ daemon

[[email protected] ~]#

 

取交集用&&

[[email protected] ~]# awk -F: ‘NR>=3 && NR<=5 {print NR,"------",$1}‘ b.txt

3 ------ daemon

4 ------ adm

5 ------ lp  123()17  

[[email protected] ~]#

 

或者||

[[email protected] ~]# awk -F: ‘NR<=2 || NR>=7 {print NR,"-----",$1}‘ b.txt

1 ----- root

2 ----- bin

7 ----- shutdown

8 ----- halt

9 ----- mail

10 ----- operator

[[email protected] ~]#

 

只输出nologin结尾的行

awk -F: ‘/nologin$/{print $1,$3,$4}‘ b.txt

 

第一段符合r开头,t结尾的UID($3)

[[email protected] ~]# awk -F: ‘$1~/^r.*t$/{print $1,$3}‘ b.txt

root 0

[[email protected] ~]#

 

用户名是root的行,信息筛选:

[[email protected] ~]# awk -F: ‘$1=="root" {print $1,$2}‘ b.txt

root x

[[email protected] ~]#

 

UID大于等于7的行:

[[email protected] ~]# awk -F: ‘$3>=7 {print $0}‘ b.txt

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

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

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

[[email protected] ~]#

 

设置参数,UID>=3

[[email protected] ~]# count=3

[[email protected] ~]# awk -v x=$count -F: ‘$3>=x {print $1,$3}‘ b.txt

adm 3

lp  123()17   4

sync 5

shutdown 6

halt 7

mail 8

operator 11

 

awk -F是默认以空格或多个空格为分割符的,上面都指定了分号做分割

从下面命令,得到网卡名是ens33

[[email protected] ~]# ifconfig

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.88.128  netmask 255.255.255.0  broadcast 192.168.88.255

        inet6 fe80::8ef4:d045:bb20:5d0a  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:56:c5:2f  txqueuelen 1000  (Ethernet)

 

取IP就是第2行第2个

[[email protected] ~]# ifconfig | awk ‘NR==2 {print $2}‘

192.168.88.128

[[email protected] ~]#

保存到IP中,用``

[[email protected] ~]# ip=`ifconfig | awk ‘NR==2 {print $2}‘`

[[email protected] ~]# echo $ip

192.168.88.128

[[email protected] ~]#

 

把文件排序和排重,排重前一定要先排序

[[email protected] ~]# cat d.txt|sort|uniq

2222222222

4444444444

777777777777

99999999999999

aaaaaaaaaa

[[email protected] ~]#

 

-c知道去重情况

[[email protected] ~]# cat d.txt|sort|uniq -c

      1 2222222222

      1 4444444444

      1 777777777777

      3 99999999999999

     17 aaaaaaaaaa

[[email protected] ~]#

 

除了awk可以取数据外,还有个简单的方法:cut,不过没有awk那么好,-d:是以冒号为分割,-f1是取第1个

[[email protected] ~]# cat b.txt|cut -d: -f1

root

bin

daemon

adm

lp  123()17 

sync

shutdown

halt

mail

operator

 

-f1是取第1部分和第3部分

[[email protected] ~]# cat b.txt|cut -d: -f1,3

root:0

bin:1

daemon:2

adm:3

lp  123()17  :4

sync:5

shutdown:6

halt:7

mail:8

operator:11

[[email protected] ~]#

 

文件大小:

[[email protected] ~]# du -sh /boot/

126M       /boot/

[[email protected] ~]#

 

把/根目录下,name中,全部包含txt的文件都找出来,用find

[[email protected] ~]# find / -name "*.txt"

 

把/根目录下,type类型为f都找出来

[[email protected] ~]# find / -type f

 

把/根目录下,大小超过300M的文件找出来

[[email protected] ~]# find / -size +300M

/proc/kcore

find: ‘/proc/4506/task/4506/fd/6’: 没有那个文件或目录

find: ‘/proc/4506/task/4506/fdinfo/6’: 没有那个文件或目录

find: ‘/proc/4506/fd/6’: 没有那个文件或目录

find: ‘/proc/4506/fdinfo/6’: 没有那个文件或目录

 

检查下是否大于300M

[[email protected] ~]# ls -lh /proc/kcore

-r-------- 1 root root 128T 12月  3 22:23 /proc/kcore

[[email protected] ~]#

 

把/根目录下,小于1k的文件找出来

[[email protected] ~]# find / -size -1k

 

把/根目录下,大于10M,小于15M的文件

[[email protected] ~]# find / -size +10M && -15M

 

文件名带txt,大小小于1K

[[email protected] ~]# find -name "*.txt" -size -1k -type f

./22.txt

./Z.txt

./[1..3].txt

./{4...9}.txt

[[email protected] ~]#

 

test文件夹下,包含子文件夹,把正文中带root的文件和内容都找出来,文件名带root不算。

先把文件复制过来:

[[email protected] test]# cp /root/d.txt .

。。。然后

[[email protected] test]# egrep -r ‘root‘ /test

/test/a.txt:root

/test/b.txt:root:x:0:0:root:/root:/bin/bash

/test/b.txt:operator:x:11:0:operator:/root:/sbin/nologin

/test/son/tt.txt:root

/test/c.txt:1rootroot

[[email protected] test]# ls

a.txt  b.txt  c.txt  d.txt  root.txt  son

[[email protected] test]#

 

上面的结果,我只想要文件名,就用-rl

[[email protected] test]# egrep -rl ‘root‘ /test

/test/a.txt

/test/b.txt

/test/son/tt.txt

/test/c.txt

[[email protected] test]#

 

设置一个脚本:把之前学的命令放到一个文件中:

[[email protected] test]# vim gaizhujiming.sh

[[email protected] test]# cat gaizhujiming.sh

read -p ‘please input your hostname:‘ name

hostnamectl set-hostname $name

[[email protected] test]#

 

改可执行权限

[[email protected] test]# chmod +x gaizhujiming.sh

[[email protected] test]#

 

执行文件:

[[email protected] test]# ./gaizhujiming.sh

please input your hostname:www

[[email protected] test]# hostname

www

[[email protected] test]#

 

 

 

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

AWK

AWK用法详解

linux awk

Linux-AWK

新awk整理

awk编程