一、grep
grep(Global search regular expression and print out the line)全面搜索研究正则表达式并显示出来。
grep可以根据用户指定的“模式”对目标文件进行匹配检查,打印匹配到的行,由正则表达式或者字符及基本文本字符所编写的过滤条件。
1、grep的格式
grep 匹配条件 处理文件
示例:
grep root passwd ##查找包含root的关键词的行
grep ^root passwd ##查找passwd中以root开头的行
grep root$ passwd ##查找passwd中以root结尾的行
grep -i root passwd ##忽略大小写查找root所在的行
示图:查找包含root的关键词的行
2、grep拓展正则表达式(+E)
grep -E "root\>" passwd ##模糊匹配以root结尾的
grep -E "\<root\>" passwd ##精确匹配root字符
grep -E "\<root" passwd ##模糊匹配以root开头
grep -E "^\<root\>" passwd ##精确匹配以root开头
grep -E "\<root\>$" passwd ##精确匹配以root结尾
grep -E -i "\<root\>" passwd ##忽略大小写
实验环境:
[root@server5 mnt]# cp /etc/passwd .
[root@server5 mnt]# vim passwd
<1>模糊匹配以root结尾
<2>精确匹配root字符
<3>模糊匹配以root开头
<4>精确匹配以root开头
<5>精确匹配以root结尾
<6>忽略大小写
3、grep中…的使用(贪婪匹配)
grep -E "x.." file ##含有x..,其中.表任意字符
grep -E "x..\>" file ##以x..结尾
grep -E "\<x..\>" file ##精确匹配x..
grep -E "\<x....\>" file ##精确匹配x....
grep -E "\<x...y\>" file ##精确匹配x...y
grep -E "...y\>" file ##以...y结尾
grep -E "...y" file ##含有...y
<1>匹配含有x…,其中.表任意字符
<2>匹配以x…结尾
<3>精确匹配x…
<4>精确匹配x…
<5>精确匹配x…y
<6>匹配以…y结尾
<7>匹配含有…y
4、grep中字符的匹配次数设定
* 字符出现0-任意次
\? 字符出现0-1次
\+ 字符出现1-任意次
示例:
grep -E "x*y" file ##含xy整体且x出现0-任意次
grep -E "x?y" file ##x出现0-1次且含xy整体
grep -E "\<x?y" file ##以x开头、包含xy整体且x出现1-2次
grep -E "\<x{2}y" file ##以x开头、包含xy整体且x出现2次
grep -E "\<x{,2}y" file ##以x开头、包含xy整体且x出现0-2次
grep -E "\<x{0,2}y" file ##以x开头、包含xy整体且x出现0-2次
grep -E "\<x{1,2}y" file ##以x开头、包含xy整体且x出现1-2次
grep -E "\<x{2,}y" file ##以x开头、包含xy整体且x出现2-任意次
grep -E "\<x{1,}y" file ##以x开头、包含xy整体且x出现1-任意次
grep -E "\<x+y" file ##以x开头、包含xy整体且x出现1-任意次
grep -E "xy+" file ##含xy整体且y出现1-任意次
grep -E "(xy)+" file ##xy整体出现1-任意次
grep -E "(xy)+\>" file ##xy整体出现1-任意次,且以xy结尾
实验环境:
[root@server5 mnt]# vim file
<1>含xy整体且x出现0-任意次
<2>x出现0-1次且含xy整体
<3>以x开头、包含xy整体且x出现1-2次
<4>以x开头、包含xy整体且x出现2次
<5>以x开头、包含xy整体且x出现0-2次
<6>以x开头、包含xy整体且x出现0-2次
<7>以x开头、包含xy整体且x出现1-2次
<8>以x开头、包含xy整体且x出现2-任意次
<9>以x开头、包含xy整体且x出现1-任意次
<10>以x开头、包含xy整体且x出现1-任意次
<11>含xy整体且y出现1-任意次
<12>xy整体出现1-任意次
<13>xy整体出现1-任意次,且以xy结尾
实例:显示ip的脚本ip_show.sh
[root@server5 mnt]# vim ip_show.sh
#!/bin/bash
ifconfig eth0 | grep -E "inet\>" | cut -d " " -f 10
[root@server5 mnt]# chmod +x ip_show.sh
测试:
[root@server5 mnt]# /mnt/ip_show.sh
172.25.254.105
二、sed行编译器
sed行编译器用来操作ASCII码的文本,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,可以指定仅仅处理那些行。sed符合模式条件的处理,不符合的不处理,处理完成之后把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,知道文件末尾,不对原文件内容作修改。
1.sed两种命令格式
sed 参数 命令 file
sed 参数 -f 脚本 file
2、p模式操作(显示/打印屏幕)
[root@server5 mnt]# sed -n '/#/p' fstab ##将fstab文件中含有#的行显示,n:显示输出结果
[root@server5 mnt]# sed -n '/#/p' -i fstab ##将fstab文件中含有#的行输入到fstab中,i:输入
[root@server5 mnt]# sed -n '/UUID/p' fstab ##显示fstab中含有UUID的行
[root@server5 mnt]# sed -n '/UUID$/p' fstab ##显示fstab中以UUID结尾的行
[root@server5 mnt]# sed -n '/^UUID/p' fstab ##显示fstab中以UUID开头的行
[root@server5 mnt]# cat -n fstab | sed -n '5p' ##显示fstab中第5行
[root@server5 mnt]# cat -n fstab | sed -n '3,5p' ##显示fstab中第3-5行
[root@server5 mnt]# cat -n fstab | sed -n '3,5!p' ##显示fstab中除第3-5行外所有的行
[root@server5 mnt]# cat -n fstab | sed -ne '3p' -ne '5p' ##显示fstab中第3、5行
[root@server5 mnt]# cat -n fstab | sed -ne '3p;5p;8p' ##显示fstab中第3、5、8行
实验环境:
[root@server5 mnt]# cp /etc/fstab .
[root@server5 mnt]# vim fstab ##添加如下内容
hasadkhsdlklkajddsalkdhkj=UUID
[root@server5 mnt]# cat -b fstab ##显示行号
<1>显示fstab中第5行
<2>显示fstab中第3-5行
<3>显示fstab中除第3-5行外所有的行
<4>显示fstab中第3、5行
<5>显示fstab中第3、5、8行
<6>显示fstab中含有UUID的行
<7>显示fstab中以UUID结尾的行
<8>显示fstab中以UUID开头的行
<9>将fstab文件中含有#的行显示,n:显示输出结果
<10>将fstab文件中含有#的行输入到fstab中,i:输入
3、d模式操作(删除)
sed '/^#/d' fstab ##删除fstab中#开头的行并显示在屏幕
sed '1,4d' fstab ##删除fstab中第1到4行并显示其他行
sed '/^UUID/!d' fstab ##除以UUID开头的行,其余行全部删除
sed '/^UUID/d' fstab ##删除以UUID开头的行并显示
sed '/UUID$/d' fstab ##删除fstab中UUID结尾的行并显示
cat -n fstab | sed '3d;5d;8d' ##删除3、5、8行
<1>删除以UUID开头的行并显示
<2>删除3、5、8行
<3>除以UUID开头的行,其余行全部删除
4、a模式操作(添加)
[root@server5 mnt]# sed '/UUID$/a hello' fstab ##在以UUID结尾的行后,另起一行添加hello
[root@server5 mnt]# sed '/UUID$/a hello sed' fstab ##在以UUID结尾的行后,另起一行添加hello sed
[root@server5 mnt]# sed '/UUID$/a hello\nsed' fstab ##在以UUID结尾的行后,另起一行添加hello,再另起一行添加sed
[root@server5 mnt]# sed '/UUID$/a hello\nsed\ntest' fstab ##在以UUID结尾的行后,另起三行,分别添加hello、sed、test
<1>在以UUID结尾的行后,另起三行,分别添加hello、sed、test
<2>在以UUID结尾的行后,另起一行添加hello
<3>在以UUID结尾的行后,另起一行添加hello,再另起一行添加sed(空格)
5、s模式操作(替换)
sed '/UUID$/s/被替换的内容/新替换后的内容/' fstab ##显示fstab内容,并将某一行替换成新的一行