Shell编程、part5

本节内容

1. 三剑客简介linux

2. sed命令详解面试

3. awk命令详解正则表达式

文本处理三剑客

在 Shell 下使用这些正则表达式处理文本最多的命令有下面几个工具:shell

|                 命令 |                描述 |
| grep | 默认不支持扩展表达式,加-E 选项开启 ERE。若是不加-E 使用花括号要加转义符\{\} |
| egrep | 支持基础和扩展表达式 |
| awk | 支持 egrep 全部的正则表达式 |
| sed | 默认不支持扩展表达式,加-r 选项开启 ERE。若是不加-r 使用花括号要加转义符\{\} |

 sed详解

1. 前言

  • 咱们都知道,在Linux中一切皆文件,好比配置文件,日志文件,启动文件等等。若是咱们相对这些文件进行一些编辑查询等操做时,咱们可能会想到一些vi,vim,cat,more等命令。可是这些命令效率不高,而在linux中有三种工具:顶配awk,中配sed,标配grep。使用这些工具,咱们可以在达到一样效果的前提下节省大量的重复性工做,提升效率。编程

  • 文件内容能够是来自文件,也能够直接来自键盘或者管道等标准输入,最后的结果默认状况下是显示到终端的屏幕上,可是也能够输出到文件中。
  • 编辑文件也是这样,之前咱们修改一个配置文件,须要移动光标到某一行,而后添加点文字,而后又移动光标到另外一行,注释点东西…….可能修改一个配置文件下来须要花费数十分钟,还有可能改错了配置文件,又得返工。这仍是一个配置文件,若是数十个数百个呢?所以当你学会了sed命令,你会发现利用它处理文件中的一系列修改是颇有用的。只要想到在大约100多个文件中,处理20个不一样的编辑操做能够在几分钟以内完成,你就会知道sed的强大了。vim

2. 语法格式

sed [选项]  [sed命令]  [输入文件]数组

说明:缓存

1,注意sed软件以及后面选项,sed命令和输入文件,每一个元素之间都至少有一个空格。bash

2,sed -commands(sed命令)是sed软件内置的一些命令选项,为了和前面的options(选项)区分,故称为sed命令app

3,sed -commands 既能够是单个sed命令,也能够是多个sed命令组合。

4,input -file (输入文件)是可选项,sed还可以从标准输入如管道获取输入。

3. sed的工做原理

sed读取一行,首先将这行放入到缓存中

而后,才对这行进行处理

处理完成之后,将缓冲区的内容发送到终端

存储sed读取到的内容的缓存区空间称之为:模式空间(Pattern Space)

4. 选项说明

option[选项] 解释说明(带*的为重点)
-n (no) 取消默认的sed软件的输出,常与sed命令的p连用。*
-e (entry) 一行命令语句能够执行多条sed命令   *
-r (ruguler) 使用扩展正则表达式,默认状况sed只识别基本正则表达式  *
-i (inside) 直接修改文件内容,而不是输出到终端,若是不使用-i选项sed软件只是修改在内存中的数据,并不会影响磁盘上的文件*
sed -commands[sed命令] 解释说明(带*的为重点)
a (append) 追加,在指定行后添加一行或多行文本 *                                                      
c (change) 取代指定的行
d (delete) 删除指定的行  *  `
i (insert) 插入,在指定行前添加一行或多行文本 *
p (print) 打印模式空间内容,一般p会与选项-n一块儿使用*
特殊符号 解释说明(带*的为重点)
对指定行之外的全部行应用命令*                                                              

 sed.增.删.改.查

1. 增

这里咱们须要用到2个sed命令,分别是:

  • “a”:追加文本到指定行后,记忆方法:a的全拼是apend,意思是追加。
  • “i“:插入文本到指定行前,记忆方法:i的全拼是insert,意思是插入。

实例1:a

[root@ken ~]# sed "2a 这是新添加的一行" test
this is the first line
this is the second line
这是新添加的一行
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line
  1. 2表明指定对第2行操做,其余的行忽略
  2. a表明插入的意思,2i即在第2行前插入文本
  3. 2a后面加上空格,而后跟上你想要插入的文本便可

实例2:i

[root@ken ~]# sed "2i 我又新添加了一行" test
this is the first line
我又新添加了一行
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例3:同时增长多行(/n)

[root@ken ~]# sed "2i 这是第一条记录\n这是第二条记录\n这是第三条记录" test
this is the first line
这是第一条记录
这是第二条记录
这是第三条记录
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

2.删

  • 这个功能也是很是得有用,好比咱们想删除文件中的某些行,之前最经常使用的是vi或vim命令,但如今咱们知道了sed命令,就应该使用这个高逼格的命令完成任务了。
  • “d”:删除文本,记忆方法:d的全拼是delete,意思是删除。
  • sed软件能够对单行或多行文本进行处理。若是在sed命令前面不指定地址范围,那么默认会匹配全部行。

实例1:删除全部的行

[root@ken ~]# cp test{,.bak}
[root@ken ~]# sed 'd' test

命令说明:若是在sed命令前面不指定地址范围,那么默认会匹配全部行,而后使用d命令删除功能就会删除这个文件的全部内容

实例2:删除指定的行

[root@ken ~]# cat test.bak >test
[root@ken ~]# sed '2d' test
this is the first line
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例3:删除指定范围行

[root@ken ~]# sed '2,5d' test
this is the first line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

实例4:删除匹配的行

[root@ken ~]# sed '/sixth/d' test
this is the first line
this is the second line
this is the third line
this is the forth line
this is the fivth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line

命令说明:在sed软件中,使用正则的格式和awk同样,使用2个”/“包含指定的正则表达式,即“/正则表达式/”。

实例5:删除指定行到行尾的内容

[root@ken ~]# sed '2,$d' test
this is the first line

第二行也会被删掉

实例6:取反

1、

[root@ken ~]# sed '2,3!d' test
this is the second line
this is the third line

2、

[root@ken ~]# sed '/tenth/!d' test
this is the tenth line

3.改

  • “c”:用新行取代旧行,记忆方法:c的全拼是change,意思是替换。
[root@ken ~]# sed '2c 改过以后的第二行' test
this is the first line
改过以后的第二行
this is the third line
this is the forth line
this is the fivth line
this is the sixth line
this is the seventh line
this is the eighth line
this is the ninth line
this is the tenth line
this is sixth line

文本替换

  • 接下来讲的这个功能,有工做经验的同窗应该很是的熟悉,由于使用sed软件80%的场景就是使用替换功能。
  • 这里用到的sed命令,选项:
    “s”:单独使用–>将每一行中第一处匹配的字符串进行替换==>sed命令
    “g”:每一行进行所有替换–>sed命令s的替换标志之一(全局替换),非sed命令。
    “-i”:修改文件内容–>sed软件的选项,注意和sed命令i区别。

sed软件替换模型

sed -i ‘s/目标内容/替换内容/g’  ken.log
sed -i ‘s#目标内容#替换内容#g’

实例1:

[root@ken ~]# sed 's/line/hang/g' test
this is the first hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

命令说明:从上面命令的结果咱们就知道sed命令默认不会修改文件的内容

实例2:

[root@ken ~]# sed -i 's/line/hang/g' test
[root@ken ~]# cat test
this is the first hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

命令说明:若是想真正的修改文件内容,咱们就须要使用选项“-i”,这个要和sed命令“i”区分开来。同时咱们能够发现命令执行后的结果是没有任何输出的。

4.查

  • 这个功能也是很是得有用,好比咱们想查看文件中的某些行,之前最经常使用的是cat或more或less命令等,但这些命令有些缺点,就是不能查看指定的行。而咱们用了好久的sed命令就有了这个功能了。并且咱们前面也说过使用sed比其余命令vim等读取速度更快!
  • 这里咱们须要用到1个sed命令
  • “p”:输出指定内容,但默认会输出2次匹配的结果,所以使用-n选项取消默认输出,记忆方法:p的全拼是print,意思是打印。

实例1:

[root@ken ~]# sed '2p' test
this is the first hang
this is the second hang
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang
[root@ken ~]# sed -n '2p' test
this is the second hang

实例2:

[root@ken ~]# sed -n '2,5p' test
this is the second hang
this is the third hang
this is the forth hang
this is the fivth hang

实例3:

[root@ken ~]# sed -n '/ninth/p' test
this is the ninth hang

补充:-e多点操做

实例1:

[root@ken ~]# sed -e '2d' -e '5d' test
this is the first hang
this is the third hang
this is the forth hang
this is the sixth hang
this is the seventh hang
this is the eighth hang
this is the ninth hang
this is the tenth hang
this is sixth hang

实例2:

[root@ken ~]# sed -n -e '2p' -e '5p' test
this is the second hang
this is the fivth hang

删除注释行和空白行

第一种方法:

[root@ken ~]# cp test{,.bak}
[root@ken ~]# ls
anaconda-ks.cfg a.out ken1 test test1 test2 test.bak test.txt
[root@ken ~]# grep -v -E “(^$)|(^#)” test.bak
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
i#adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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
[root@ken ~]# grep -v -E “(^$)|(^#)” test.bak > test
[root@ken ~]# cat test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
i#adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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

第二种方法:

[root@ken ~]# sed -i -e ‘/^$/d’ -e ‘/^#/d’ test.bak
[root@ken ~]# cat test.bak
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
i#adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
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

sed用法总结

1.查找指定的字符串

例子:显示/etc/passwd中保含root的行(显示模式空间中的内容)

方法1:set '/root/p' /etc/passwd

方法2:cat /etc/passwd | sed '/root/p'

2.在指定的位置作增删

例子:删除以root为开头的行

# sed ‘/^root/d’ a.txt

例子:在包含root的行后添加一行 i am ken

# sed ‘/root/a i am ken’ a.txt

3.按行替换

例子:将5到9行的内容替换为 i am ken

# sed ‘5,9c i am ken’ a.txt

4.按照字符替换

例子:将/etc/selinux/config中的SELINUX=enforcing改为 disabled

写法1:# sed -i ‘s/SELINUX=disabled/SELINUX=enforcing/g’ config

写法2:# sed -r -i ‘s/(SELINUX=)disabled/\1enforcing/g’ config

5.查找指定的内容再作替换

例子:将以r开头的行中的oo替换为qq

# sed ‘/^r/{s/oo/qq/g}’ passwd

6.多点编辑

例子:去除文件中的注释行和空白行

# grep -v -E “(^#)|(^$)” passwd.bak >passwd

# cat passwd.bak | sed -e ‘/^#/d’ -e ‘/^$/d’ >passwd

7)取反操做

显示非1-3行

# sed -n ‘1,3!p’ passwd

awk详解

awk不只仅时linux系统中的一个命令,并且是一种编程语言,能够用来处理数据和生成报告(excel)。处理的数据能够是一个或多个文件,能够是来自标准输入,也能够经过管道获取标准输入,awk能够在命令行上直接编辑命令进行操做,也能够编写成awk程序来进行更为复杂的运用。

awk的格式

  • awk指令是由模式,动做,或者模式和动做的组合组成。
  • 模式既pattern,能够相似理解成sed的模式匹配,能够由表达式组成,也能够是两个正斜杠之间的正则表达式。好比NR==1,这就是模式,能够把他理解为一个条件。
  • 动做即action,是由在大括号里面的一条或多条语句组成,语句之间使用分号隔开。好比awk使用格式:

image

awk处理的内容能够来自标准输入(<),一个或多个文本文件或管道。

image

  • pattern既模式,也能够理解为条件,也叫找谁,你找谁?高矮,胖瘦,男女?都是条件,既模式。
  • action既动做,能够理解为干啥,找到人以后你要作什么。
    模式和动做的详细介绍咱们放在后面部分,如今你们先对awk结构有一个了解。
  • 分析awk处理过程:

读取文本第一行,首先会和匹配模式进行相匹配,若是发现第一行内容不和模式相匹配的话,就继续读取下一行
读取到第二行发现和模式相匹配,就会执行花括号里面的动做
而后继续读取下一行,发现和模式相匹配,就会执行花括号里面的动做
...
依次读取全文

awk参数

-F:指定分隔符

几个小概念

记录(record):一行就是一个记录

分隔符(field separator):进行对记录进行切割的时候所使用的字符

字段(field):将一条记录分割成的每一段

FILENAME:当前处理文件的文件名

FS(Field Separator):字段分隔符(默认是以空格为分隔符=)

NR(Number of Rrecord):记录的编号(awk每读取一行,NR就加1==)

NF(Number of Field):字段数量(记录了当前这条记录包含多少个字段==)

ORS(Output Record Separator):指定输出记录分隔符(指定在输出结果中记录末尾是什么,默认是\n,也就是换行)

OFS(Output Field Separator):输出字段分隔符

RS:记录分隔符

输出字段的表示方式

$1 $2 … $n 输出一个指定的字段

$NF 输出最后一个字段

$0 输出整条记录

awk执行过程

[root@ken ~]# awk 'NR>=2&&NR<=5{print $0}' /etc/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

命令说明: 条件NR>=2,表示行号大于等于2时候,执行{print $0}显示整行。 awk是经过一行一行的处理文件,这条命令中包含模式部分(条件)和动做部分(动做),awk将处理模式(条件)指定的行

1)awk读入第一行内容

2)判断是否符合模式中的条件NR>=2

a,若是匹配则执行对应的动做{print $0}
b,若是不匹配条件,继续读取下一行

3)继续读取下一行
4)重复过程1-3,直到读取到最后一行(EOF:end of file)

准备测试文件

[root@ken ~]# head /etc/passwd > test
[root@ken ~]# cat test
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: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

实例1:打印行号

[root@ken ~]# awk '{print NR,$0}' test
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:输出有多余5个字段的行的第三个字段

[root@ken ~]# awk -F ':' 'NF>=5{print $3}' test
0
1
2
3
4
5
6
7
8
11

实例3:输出每行行号和该行有几个字段

[root@ken ~]# awk -F ':' '{print NR,NF}' test
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7

例子4:打印出来用户名以及对应的shell类型

[root@ken ~]# awk -F “:” ‘{print $1,$NF}’ test
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
i#adm /sbin/nologin
lp /sbin/nologin
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
operator /sbin/nologin

[root@ken ~]# awk -F “:” ‘{print $1,$7}’ test
root /bin/bash
bin /sbin/nologin
daemon /sbin/nologin
i#adm /sbin/nologin
lp /sbin/nologin
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
operator /sbin/nologin

 awk进阶–正则

  • 正则表达式的运用,默认是在行内查找匹配的字符串,如有匹配则执行action操做,可是有时候仅须要固定的列来匹配指定的正则表达式,好比:我想取/etc/passwd文件中第五列{$5}这一列查找匹配mail字符串的行,这样就须要用另外两个匹配操做符,而且awk里面只有这两个操做符来匹配正则表达式。

实例1:匹配整行

[root@ken ~]# awk '/^root/' test
root:x:0:0:root:/root:/bin/bash

和下面的效果是同样的

[root@ken ~]# awk '$0~/^root/' test
root:x:0:0:root:/root:/bin/bash

注意:awk只用正则表达式的时候是默认匹配整行的即‘$0~/^root/’和‘/^root/’是同样的。

实例2:匹配一行中的某一列

[root@ken ~]# awk -F ':' '$5~/root/' test
root:x:0:0:root:/root:/bin/bash

提示:

  • $5表示第五个区域(列)
  • ~表示匹配(正则表达式匹配)
  • /root/表示匹配root这个字符串

$5~/root/表示第五个区域(列)匹配正则表达式/root/,既第5列包含root这个字符串,则显示这一行。

实例3:匹配行尾为sync

[root@ken ~]# awk -F ':' '/sync$/{print $0}' test
sync:x:5:0:sync:/sbin:/bin/sync

实例4:显示名字和登陆类型

[root@ken ~]# awk -F ':' '{print $1,$NF}' test
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

$NF:表示匹配的末尾部分,这里也能够写成$7

实战: 取出网卡IP地址(企业面试题)

[root@ken ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:99:ea:a6 brd ff:ff:ff:ff:ff:ff
    inet 172.20.10.6/24 brd 172.20.10.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 2408:84f4:86:47e1:20c:29ff:fe99:eaa6/64 scope global mngtmpaddr dynamic 
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe99:eaa6/64 scope link 
       valid_lft forever preferred_lft forever

第一种方法:

[root@ken ~]# ip a | awk -F ' +' 'NR==9{print $3}' | awk -F '/' '{print $1}'
172.20.10.6

第二种方法:

[root@ken ~]# ip a | grep -E '^ +.*inet\>.*' | awk -F ' +|/' 'NR==2{print $3}'
172.20.10.6

第三种方法:

[root@ken ~]# hostname -i | awk -F ' ' '{print $3}'
172.20.10.6

第四种方法:

[root@ken ~]# ip a | grep brd.*glo | awk -F ' +|/' '{print $3}'
172.20.10.6

第五种方法:

[root@ken ~]# ip a | grep "scope" | awk 'NR==3{print $0}' | awk -F "( |/)+" '{print $3}'
172.20.10.6

方法还有不少不少,你们若是对本身有高要求的话,要至少写出来十种以上的方法哦!

 awk特殊模式-BEGIN模式与END模式

  • BEGIN模块再awk读取文件以前就执行,通常用来定义咱们的内置变量(预约义变量,eg:FS,RS)
  • 须要注意的是BEGIN模式后面要接跟一个action操做块,包含在大括号内。awk必须在输入文件进行任何处理前先执行BEGIN里的动做(action)。咱们能够不要任何输入文件,就能够对BEGIN模块进行测试,由于awk须要先执行完BEGIN模式,才对输入文件作处理。BEGIN模式经常被用来修改内置变量ORS,RS,FS,OFS等值。

BEGIN模块

实例1:

[root@ken ~]# ifconfig eth0 | awk -F "[ :]+" 'NR==2{print $3}'
172.20.10.6
[root@ken ~]# ifconfig eth0 | awk -F "[^0-9.]+" 'NR==2{print $2}'
172.20.10.6

#上面的也能够写成
[root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[ :]+"}NR==2{print $3}'
172.20.10.6
[root@ken ~]# ifconfig eth0 | awk 'BEGIN{FS="[^0-9.]+"}NR==2{print $2}'
172.20.10.6

实例2:在读取文件以前,输出些提示性信息(表头)。

[root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}' test
username bash type
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

END模块

EHD在awk读取完全部的文件的时候,再执行END模块,通常用来输出一个结果(累加,数组结果),也能够是和BEGIN模块相似的结尾标识信息与BEGIN模式相对应的END模式,格式同样,可是END模式仅在awk处理完全部输入行后才进行处理。

实例1:

[root@ken ~]# awk -F ':' 'BEGIN{print "username","bash type"}{print $1,$NF}END{print "end of file"}' test
username bash type
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
end of file

实例2:统计包含root的行的数量

方法一:

[root@ken ~]# cat test | grep root| wc -l
2

方法二:

[root@ken ~]# cat test | grep -c root
2

方法三:

[root@ken ~]# cat /etc/passwd | awk 'BEGIN{i=0}/root/{i++}END{print i}'
2
[root@ken ~]# cat /etc/passwd | awk '/root/{i++}END{print i}'
2

总结awk执行过程

回顾一下awk的结构

awk -F 指定分隔符 ‘BRGIN{}END{}’,以下图

image

awk数组

数组构成:

数组名[元素名]=值

image

如图不难发现,awk数组就和酒店同样。数组的名称就像是酒店名称,数组元素名称就像酒店房间号码,每一个数组元素里面的内容就像是酒店房间里面的人。

实战:统计域名出现的次数(百度和搜狐面试题)

[root@ken ~]# cat test
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.qq.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.sina.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken
http://www.taobao.com/ken

方法一:

[root@ken ~]# cat test | awk -F '/+' '{print $2}' | sort | uniq -c
      7 www.qq.com
     13 www.sina.com
     25 www.taobao.com</pre>

方法二:

[root@ken ~]# cat test | awk -F '/+' '{h[$2]++}END{for (i in h) print i,h[i]}'
www.sina.com 13
www.qq.com 7
www.taobao.com 25

awk用法总结

1. **结合内置变量,打印指定的几行,以及字段数量**

例子;输出有多余5个字段的行的第三个字段

# cat a.sh | awk -F “:” ‘NF>=5{print $3}’

例子:输出每行行号和该行有几个字段

# cat a.sh | awk -F “:” ‘{print NR,NF}’

例子:输出用户名,要求全部用户显示在同一行,并且用空格分隔

# cat mypwd | awk ‘BEGIN{FS=”:”; ORS=” “}{print $1}’

2. **结合正则来匹配一行或者某个字段**

例子:输出用户名以s为开头的用户的uid

# cat mypwd | awk -F “:” ‘/^s/{print $}’

例子:输出第五个字段是以t为结尾的用户的姓名

# cat mypwd | awk -F “:” ‘$5~/t$/{print $1}’

3. **采用比较符号来进行打印指定的某些行**

例子:实现仅仅输出3-5的内容,每行前面添加一个行号

# cat mypwd | awk ‘NR>=3&&NR<=5{print NR,$1}’

或

# cat mypwd | awk ‘NR==3,NR==5{print NR,$1}’

例子:实现仅仅输出3 和 5 和 7行的内容,每行前面添加一个行号

# cat mypwd | awk ‘NR==3||NR==5||NR==7{print NR,$1}’

4. **END**

例子:统计mypwd中以#开头的行有多少行

# cat mypwd | awk ‘BEGIN{n=0}/^#/{n+=1}END{print n}’

统计:mypwd中,以:为分隔符,字段数量在3-5的行的数目

# cat mypwd  | awk ‘BEGIN{FS=”:”}NF>=3&&NF<=5{n+=1}END{print n}’

5. **ip**

例子:统计IP

[root@ken]# cat url.txt | awk -F “/+” ‘{urls[$2]++}END{for(key in urls)print key, urls[key]}’

www.baidu.com 12

haha.baidu.com 1

ftp.baidu.com 6

mail.baidu.com 7
相关文章
相关标签/搜索