grep
1、grep
  • grep文本过滤器,是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。
  • grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到屏幕,不影响原文件内容。
  • grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
  • egrep的命令只跟grep有很小不同。egrep是grep的扩展,支持更多的re元字符,它把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。它功能更强,可以通过-E命令行选项来使用egrep的功能。
正则表达式:
  • 正则表达式,又称规则表达式计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
  • 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。

2、grep 命令
1 grep使用格式

grep [options] PATTERN[FILE…]

grep root passwd #过滤出包含root的行
grep “^root” passwd # 过滤出以root开头的行
grep “root$” passwd #过滤出以root结尾的行
grep -i root passwd # -i 不区分大小写,过滤出包含root的行
grep -E “^root|root$” passwd # 过滤出以root结尾和者开头的行

[root@localhost mnt]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost mnt]# grep "^root" passwd
root:x:0:0:root:/root:/bin/bash

[root@localhost mnt]# grep "root$" passwd

[root@localhost mnt]# grep -i root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost mnt]# grep -E "^root$" passwd
2 参数
在这里-i:忽略大小写    
-v:显示没有被模式匹配到的行 反向过滤
-o:只显示被模式匹配到的字符串,不显示行
-E:使用扩展正则表达式
-c:统计匹配行数
-q:静默,无任何输出
-n:显示匹配结果所在的行号
-A n:表示显示该行及其后n行
-B n:表示显示该行及其前n行
-C n:表示显示该行及其前后各n行

shell 遍历grep返回的行 shell判断grep返回_sed

shell 遍历grep返回的行 shell判断grep返回_搜索_02

shell 遍历grep返回的行 shell判断grep返回_sed_03

shell 遍历grep返回的行 shell判断grep返回_sed_04

shell 遍历grep返回的行 shell判断grep返回_正则表达式_05

3 grep 可以使用的正则表达式

^关键字       #关键字及开头的行
关键字$       # 关键字结尾的行
\<关键字      # 关键字前面没有字符的
关键字\>      # 关键字后面没有字符
\<关键字\>    # 关键字前后都没有字符
"r...t"      #以r开始,以t结束,中间存在三个字符。
“r.....”     #以r开头 后面存在5个字符
“.....t”     #以t结尾,前面存在5个字符
“.”          #过滤非空的行

shell 遍历grep返回的行 shell判断grep返回_shell 遍历grep返回的行_06


shell 遍历grep返回的行 shell判断grep返回_搜索_07

4 grep中字符的匹配次数设定
*           #字符出现 [0-任意次 ]
?           #字符出现 [ 0-1次   ]
+           #字符出现 [ 1-任意次]
{n}         #字符出现 [ n次 ] 
{m,n}       #字符出现 [ 最少出现m次,最多出现n次]
{0,n}       #字符出现 [ 0-n次]
{m, }       #字符出现 [ 至少m次]
(xy)\{n}    #关键xy字出现[n次]
.*          #关键字之前匹配任意字符任意次

shell 遍历grep返回的行 shell判断grep返回_sed_08


shell 遍历grep返回的行 shell判断grep返回_正则表达式_09

sed

  • sed:Stream EDitor,流编辑器,默认只处理模式空间,,不处理原数据,如果你处理的数据是针对行进行处理的,可以使用sed。
  • sed逐行对文本进行处理。对行处理的功能比较强大。
1、sed 命令格式
调用sed 命令的两种形式:
sed 【参数】 ‘动作’ 目标文件
sed 【参数】 -f 记录动作的文件 目标文件

参数

p : 显示
d :  删除
a : 添加
c : 替换
w : 写入
i : 插入

p 打印模式

sed -n '/:/p' passwd        #显示有:的行的内容
sed -n '/root$/P'passwd        #显示root结尾的行的内容
sed -n '/^root/P' passwd       #显示root开头的行的内容
sed -n '1,4p'passwd            #显示第一行到第四行的内容
sed -n '1,4!p' passwd         #显示除了1-4行都显示
-n                             # 不输出模式空间中的内容
!                             # 非  除过之外
sed  -n  2p   passwd           # 显示第二行

shell 遍历grep返回的行 shell判断grep返回_sed_10

shell 遍历grep返回的行 shell判断grep返回_shell 遍历grep返回的行_11


d 删除模式
d模式不能使用参数 -n 否则没有任何内容输出。

sed '/^root/d' passwd                    #删除root开头的行的内容
sed '/root$/d' passwd                  #删除root结尾的行的内容
sed '/^$/d' passwd                       #删除空行
sed '1,4d' passwd                       #删除1-4行的内容
sed '/^root/!d' passwd                   #删除除root开头外的行的内容
sed -e ‘/^root/d’ -e '/root$/d' passwd   #删除root开头 以及root结尾的行  执行两个动作使用-e 
sed  '/^root/d';'/root$/d' passwd        #删除root开头 以及root结尾的行  执行两个动作使用;

shell 遍历grep返回的行 shell判断grep返回_搜索_12


shell 遍历grep返回的行 shell判断grep返回_正则表达式_13

shell 遍历grep返回的行 shell判断grep返回_正则表达式_14

a 添加模式

sed '/^root/ahello toto' passwd  #在root开头的行的下一行添加‘hello toto’
sed '/^root/ahello\ntoto' passwd  #在root开头的行的后面添加‘hello\ntoto’
\n        #换行符

shell 遍历grep返回的行 shell判断grep返回_搜索_15


i 插入模式

sed  '/^root/ihello toto' passwd	#在root开头的行的前面上一行添加‘hello  toto’

shell 遍历grep返回的行 shell判断grep返回_shell 遍历grep返回的行_16


c 修改替换模式

sed '/^root/chello toto '  passwd		#将root开头的行的内容用‘hello  toto ’替换

shell 遍历grep返回的行 shell判断grep返回_搜索_17

w 文件整合模式

sed '/^root/w /mnt/toto' passwd     #将fstab中以root为开头的行整合到toto中,覆盖源文件内容。
sed -n '/^root/w /mnt/toto' passwd   #不显示模式空间中的内容
sed '6r/mnt/toto' passwd   #将/mnt/toto中的所有内容整合到passwd的第六行 后面

shell 遍历grep返回的行 shell判断grep返回_搜索_18

2、sed的其他用法

sed -n '/^root/=' passwd                       # 显示以root开头的行的行号
sed -n -e '/^root/p' -e '/^root/=' passwd      #显示以root开头的行并显示行号
sed 's/:/##/g' passwd                          # 替换所有行所有列中的:为##
sed -f userfile  passwd                        # 按照文件userfile中的规则处理
sed 's/^\//#/' fstab                           # 将每一行处于行首的/ 提哈替换成#
sed 's/\//#/' fstab                            #将每一行的第一个/ 替换成#
sed 's@^/@#@g' fstab                           # 将每一行的行首的/替换成#
sed 's/\//#/g' fstab                           # 将全文的/ 替换成#

shell 遍历grep返回的行 shell判断grep返回_sed_19


shell 遍历grep返回的行 shell判断grep返回_正则表达式_20

shell 遍历grep返回的行 shell判断grep返回_搜索_21

shell 遍历grep返回的行 shell判断grep返回_sed_22


shell 遍历grep返回的行 shell判断grep返回_sed_23

sed 'G'  passwd ##在每行行后插入一个空白行
sed '$!G' passwd ##在除最后一行外的每行行后插入一个空白行
sed '=' passwd | sed 'N;s/\n//' ##每行行前添加行号
sed -n '$p' passwd ##显示最后一行

shell 遍历grep返回的行 shell判断grep返回_正则表达式_24

shell 遍历grep返回的行 shell判断grep返回_正则表达式_25

shell 遍历grep返回的行 shell判断grep返回_sed_26

shell 遍历grep返回的行 shell判断grep返回_正则表达式_27

awk

报告生成器
awk处理机制:awk会逐行处理文本,支持在处理第一行之前做一些准备工作,以及在处理完最后一行做一些总结性质的工作,awk再对列的处理上比较占据优势。在命令格式上分别体现如下:

  • BEGIN{}:读入第一行文本之前执行,一般用来初始化操作
  • {}:逐行处理,逐行读入文本执行相应的处理,是最常见的编辑指令
  • END{}:处理完最后一行文本之后执行,一般用来输出处理结果
awk '{ print FILENAME }' passwd                 #逐行输出文件名称
awk '{print NR}' passwd                         # 逐行输出行号
awk '{print NF}' passwd                         # 逐行输出每一行的列数
awk -F : '{print NF}' passwd                    # -F : 设定列分隔符为: 并输出每一行的列数
awk 'BEGIN{print "name"}{print NR}' passwd      # 开始之前输出name 输出行号
awk '{print NR}END{print "welcome"}' passwd     # 输出行号 结束时候输出 welcome
awk -F : 'BEGIN{print "name"}{print $1}END{print "welcome"}' passwd    # 开始输出name  结束输出welcom  逐行输出第一列
awk -F : '/^root/{print $1}' passwd              # 输出以root 开头的行的第一列

shell 遍历grep返回的行 shell判断grep返回_sed_28


shell 遍历grep返回的行 shell判断grep返回_shell 遍历grep返回的行_29


shell 遍历grep返回的行 shell判断grep返回_正则表达式_30


shell 遍历grep返回的行 shell判断grep返回_shell 遍历grep返回的行_31


shell 遍历grep返回的行 shell判断grep返回_sed_32

使用示例:

[root@client mnt]# awk -F : '{print $1,$2}' passwd     # 输出第一列 和第二列
root x
bin x
halt x

[root@client mnt]# awk -F : 'BEGIN{N=O}{N++ }END{print N}' passwd    # 计算文件共有多少行
3