Linux学习笔记——shell文本处理正则表达式

1.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

这里写图片描述


2.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中第35行
[root@server5 mnt]# cat -n fstab | sed -ne '3p;5p;8p'     ##显示fstab中第358

实验环境:

[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(空格)
这里写图片描述

猜你喜欢

转载自blog.csdn.net/hannah_zh/article/details/80307165