Linunx:shell的正则表达式

正则表达式

正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。
许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成“regex”,单数有regexp、regex,复数有regexps、regexes、regexen。

文本处理工具

grep
sed
awk

一·grep

文本过滤命令:grep是一种文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行;
grep:由正则表达式或者字符及基本文本字符所编写的过滤条件;

1、grep中字符的匹配位置设定

[root@desktop ~]# cp /etc/passwd   /mnt/
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
[root@desktop mnt]# vim passwd      ##文件过长,为了实验,可以删除一些
[root@desktop mnt]# grep ^root passwd    ##过滤以root开头的行
[root@desktop mnt]# grep root$ passwd    ##过滤以root结尾的行
[root@desktop mnt]# grep -i ^root passwd    ##忽略大小写字母,过滤以root开头的行
-i:忽略大小写
[root@desktop mnt]# grep -i root$ passwd    #忽略大小写字母,过滤以root结尾的行#
[root@desktop mnt]# grep -i -E "^root|root$" passwd     ##过滤root开头结尾的行
-E:拓展正则表达式
[root@desktop mnt]# grep -v -i -E "^root|root$" passwd | grep root   ##过滤root为中间的行
-v:屏蔽

这里写图片描述
这里写图片描述

2、grep匹配字符出现次数

[root@desktop mnt]# vim test    ##编辑文件
[root@desktop mnt]# cat test 
[root@desktop mnt]# grep 'r..t' test    ##中间有几个字符就有几个点
[root@desktop mnt]# grep 'r...t' test 
[root@desktop mnt]# grep 'r....t' test 
[root@desktop mnt]# grep 'r..t' test 

编辑内容
这里写图片描述
这里写图片描述

3、grep的匹配次数设定

*       字符出现0-任意次

-E ? 字符出现0-1次
-E + 字符出现1-任意次
-E {m,n} 字符出现最少出现m次,最多出现n次
-E {,n} 字符出现0-n次
-E {m,} 字符出现至少m次
-E r…> r后2个字符结尾
.* 关键字之间匹配任意字符

[root@desktop mnt]# vim test     ##稍作一下修改
[root@desktop mnt]# cat test     
[root@desktop mnt]# grep -E 'r?t' test    ##开头为"r"结尾为"t"的字符出现0-1次
[root@desktop mnt]# grep -E 'r*t' test    ##开头为"r"结尾为"t"的字符出现0-任意次
[root@desktop mnt]# grep -E 'ro?t' test   ##开头为"ro"结尾为"t"的字符出现0-1次
[root@desktop mnt]# grep -E 'ro{1,3}t' test   ##开头为"r"结尾为"t"的字符最少出现1次,最多出现3次
[root@desktop mnt]# grep -E 'ro+t' test    ##开头为"r"结尾为"t"的字符出现1-任意次
[root@desktop mnt]# grep -E 'root{1,}' test   ##开头为"root"字符至少出现1次
[root@desktop mnt]# grep -E 'root{,2}' test    ##开头为"root"字符最多出现2次
[root@desktop mnt]# grep -E 'r.*' test    ##关键字之间匹配任意字符
[root@desktop mnt]# grep -E 'r..\>' test   ##r2个字符结尾

修改后的内容
这里写图片描述
这里写图片描述
这里写图片描述

实验一:在系统当中找到可以登录的用户

[root@desktop mnt]# cat /etc/shells   ##查看shell
[root@desktop mnt]# vim show_loginuser.sh   ##编写脚本
[root@desktop mnt]# chmod +x show_loginuser.sh    ##赋予权限
[root@desktop mnt]# /mnt/show_loginuser.sh   ##绝对调用
[root@desktop mnt]# useradd -s /bin/tcsh user1    ##user1在tcsh这个shell中建立用户
[root@desktop mnt]# su - user1   ##切换用户
[user1@desktop ~]$ logout
[root@desktop mnt]# /mnt/show_loginuser.sh    ##再次调用

编写脚本
这里写图片描述
这里写图片描述
这里写图片描述

二·sed

行编辑器:用来操作纯ASCII码的文本。
原理:处理时,把当前处理的行存储在“模式空间”(临时缓冲区),
符合模式条件的处理,不符合条件的不处理,处理完成后把缓冲区内
容送往屏幕;接着处理下一行,不断重复,直至文件结束;
多个条件用-e连接

1、p模式(显示)

[root@desktop mnt]# cp /etc/fstab /mnt/    ##复制到/mnt
[root@desktop mnt]# cat fstab 
[root@desktop mnt]# sed -n '/^#/p' fstab    ##显示以"#"开头的
[root@desktop mnt]# sed -n '/^#/!p' fstab   ##不显示以"#"开头的
[root@desktop mnt]# cat -n fstab |sed -n '2,6p'   ##显示第2-6行
[root@desktop mnt]# cat -n fstab |sed -n -e '2p;6p'    ##显示第2行和第六行
[root@desktop mnt]# cat -n fstab |sed -n -e '2!p;6!p' |uniq -d   ##显示除了第2行和第六行
[root@desktop mnt]# cat -n fstab |sed -n -e '2!p;6!p' |uniq -u   ##显示第二行和第六行

这里写图片描述
这里写图片描述

实验二:用脚本建立文件中的用户以及使用文件对应的密码

[root@desktop mnt]# vim userfile    ##用户
[root@desktop mnt]# cat userfile     
user1
user2
user3
[root@desktop mnt]# vim passfile    ##密码
[root@desktop mnt]# cat passfile 
user1123
user2123
user3123
[root@desktop mnt]# vim user_create.sh   ##编辑脚本
[root@desktop mnt]# sh user_create.sh userfile passfile    ##调用
[root@desktop mnt]# id user1    查看user1用户
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@desktop mnt]# su user1   ##切换user1用户
[user1@desktop /mnt]$ su user2   ##切换user2用户

编辑脚本内容
这里写图片描述
这里写图片描述

2、d模式(删除)

[root@desktop mnt]# cat -n fstab |sed -e '2d;6d'    ##删除第二行和第六行
[root@desktop mnt]# cat -n fstab |sed -e '2,6d'     ##删除第二到第六行
[root@desktop mnt]# sed -e '/^$/d' fstab            ##删除空行
[root@desktop mnt]# sed -e '/UUID/d' fstab          ##删除UUID行
[root@desktop mnt]# sed -e '/UUID/!d' fstab         ##不删除UUID行

这里写图片描述
这里写图片描述

3、a模式(添加)

添加默认在关键字下面

[root@desktop mnt]# vim westos
[root@desktop mnt]# cat westos 
hello
[root@desktop mnt]# sed '/hello/aworld' westos      ##添加world
[root@desktop mnt]# sed 's/hello/hello world/g' westos    ##更换hello为hello world
[root@desktop mnt]# sed '/hello/aworld\nwestos' westos    ##添加world,westos(\n:换行)

这里写图片描述

4、i模式(插入)

插入是在关键字上面

[root@desktop mnt]# sed '/hello/iworld\nwestos' westos   ##插入world,westos在hello上面

这里写图片描述

5、c模式(替换)

[root@desktop mnt]# vim westos
[root@desktop mnt]# cat westos 
hello
[root@desktop mnt]# sed '/hello/chello world' westos    ##替换hello为hello world
[root@desktop mnt]# sed '/hello/chello\nworld' westos   ##替换hello为hello 并且换行world
[root@desktop mnt]# sed '/hello/cwestos\nworld' westos  ##替换hello为westos,并且换行为westos

这里写图片描述

6、w模式(写)

[root@desktop mnt]# sed -n '/bash$/p' passwd  > file   ##将bash结尾的行重定向到文件
[root@desktop mnt]# cat file 
[root@desktop mnt]# rm -fr file
[root@desktop mnt]# sed -n '/bash$/wfile' passwd    ##将bash结尾的行重定向到文件
[root@desktop mnt]# cat file
w和>的区别:w做的是一个命令,>做的是两个命令;w的效率要比>高

这里写图片描述

[root@desktop mnt]# sed '/hello/p' westos
[root@desktop mnt]# sed '/hello/=' westos     ##"="显示行号
[root@desktop mnt]# echo `sed '/hello/=' westos`   ##将输出结果放到一行
[root@desktop mnt]# sed '6r westos' fstab    ##把westos文件中的内容写在fstab文件的第六行下面

这里写图片描述

7、sed其他用法

(1)、-n:只显示匹配空间的内容

[root@desktop mnt]# sed -n '/^UUID/=' fstab    ##只显示行数
[root@desktop mnt]# sed  '/^UUID/=' fstab    ##显示行数和内容
[root@desktop mnt]# sed '=' fstab | sed 'N;s/\n//g'    ##在文件前面加行号
[root@desktop mnt]# sed 'G' fstab    ##在内容的每一行后面加一个空行
[root@desktop mnt]# sed -n '$p' fstab   ##显示最后一行

这里写图片描述
这里写图片描述
这里写图片描述

(2)替换模式

[root@desktop mnt]# rm -fr *
[root@desktop mnt]# cp /etc/passwd /mnt/
[root@desktop mnt]# ll
[root@desktop mnt]# vim passwd    ##删除一些内容
[root@desktop mnt]# sed 's/nologin/westos/g' passwd    ##将全文中的nologin替换为westos

这里写图片描述

[root@desktop mnt]# sed '3,5s/nologin/westos/g' passwd    ##将全文中的3-5行的nologin替换为westos

这里写图片描述

[root@desktop mnt]# sed '/adm/,/sync/s/nologin/westos/g' passwd    ##将全文中/adm/与/sync/关键字间的nologin替换为westos

这里写图片描述

[root@desktop mnt]# sed -e '/adm/,/sync/s/nologin/westos/g;s/sbin/linux/g' passwd   ##将全文中/adm/与/sync/关键字间的nologin替换为westos,sbin替换为linux

这里写图片描述

[root@desktop mnt]# vim file
[root@desktop mnt]# cat file
s/sbin/westos/g    ##全文中sbin替换为westos
s/nologin/linux/g   ##全文中nologin替换为linux
[root@desktop mnt]# sed -f file passwd
[root@desktop mnt]# sed -f file -i passwd    ##-i会覆盖内容

这里写图片描述

实验三、改变阿帕奇端口

root@desktop mnt]# vim install_apache.sh   ##编辑脚本
[root@desktop mnt]# netstat -antlupe | grep 80    ##查看80端口
[root@desktop mnt]# sh install_apache.sh 80   ##运行脚本
[root@desktop mnt]# netstat -antlupe | grep http   ##查看http端口

编辑脚本内容:
这里写图片描述
这里写图片描述

三·awk报告生成器

awk处理机制:逐行处理文本,支持在处理第一行之前做一些准备工作,以及在处理完最后一行做一些总结性质的工作
-F:指定分隔符

1、awk的基本用法

[root@desktop mnt]# awk -F ":" '{print $1}' passwdroot   ##打印第一列
[root@desktop mnt]# awk -F ":" 'BEGIN{print "NAME"}{print $1}' passwd    ##以:为分隔符,处理前打印NAME,打印第1列
[root@desktop mnt]# awk -F ":" 'BEGIN{print "NAME"}{print $1}END{print "END"}' passwd    ##以:为分隔符,处理前打印 NAME ,结尾为END,打印第1列
[root@desktop mnt]# awk -F ":" 'BEGIN{print "NAME"}{print NR}END{print "END"}' passwd   ##以:为分隔符,处理前打印 NAME ,结尾为END,打印第1列,处理后打印行数(NF列)
[root@desktop mnt]# awk '/bash$/{print}' passwd    ##以:为分隔符,打印以bash结尾行
[root@desktop mnt]# awk -F ":" '/bash$/{print $1}' passwd   ##打印以"bash"结尾的第一行
[root@desktop mnt]# awk -F ":" '{print NR$0}' passwd   ##打印行数,0代表所有

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

实验四:登录系统的用户有多少个(用awk命令写)

[root@desktop mnt]# awk -F : 'BEGIN{N=0}/bash$/{N++}END{print N}' passwd

这里写图片描述

2、awk的基本命令

[root@desktop mnt]# awk '/^ro/{print}' passwd    ##开头为ro的
[root@desktop mnt]# awk '/^[a-d]/{print}' passwd   ##以开头字母为a-d
[root@desktop mnt]# awk '/^[^a-d]/{print}' passwd   ##不是以开头字母为a-d
[root@desktop mnt]# awk '/^a|nologin$/{print}' passwd   ##以a开头或以nologin结尾
[root@desktop mnt]# awk '/^a/&&/nologin$/{print}' passwd  ##以a开头并且以nologin结尾
[root@desktop mnt]# awk '/^r/&&/bash$/{print}' passwd    ##以r开头并且以bash结尾
[root@desktop mnt]# awk '/^r/||/bash$/{print}' passwd   ##以r开头或以bash结尾
[root@desktop mnt]# awk -F ":" '$5~/^a/{print}' passwd   ##以“:”为分隔符,第五列以a开头的
[root@desktop mnt]# awk -F ":" '$1~/^a/{print}' passwd   ##以“:”为分隔符,第一列以a开头的
[root@desktop mnt]# awk -F ":" '$1~/^r/{print}' passwd   ##以“:”为分隔符,第一列以r开头的
[root@desktop mnt]# awk -F ":" '$1!~/^r/{print}' passwd   ##以“:”为分隔符,不是第一列以r开头的

这里写图片描述
这里写图片描述

实验五·登陆系统且家目录不是/home的用户个数

[root@desktop mnt]# vim home.sh   
[root@desktop mnt]# sh home.sh 

编辑脚本内容
这里写图片描述这里写图片描述

实验六·设备eth0的IP

[root@desktop mnt]# vim ip.sh
[root@desktop mnt]# sh ip.sh 
[root@desktop mnt]# ifconfig eth0 | awk -F " " '/inet\ /{print $2}'
(两种命令都可行,可以直接执行,也可以编辑脚本执行)

编辑脚本内容
这里写图片描述
这里写图片描述

实验七·统计文件有多少行

[root@desktop mnt]# awk 'BEGIN{n=0}{n++}END{print n}' /etc/passwd    ##命令直接执行
[root@desktop mnt]# vim ll.sh    ##脚本执行
[root@desktop mnt]# sh ll.sh 

编辑脚本内容
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/le_anny/article/details/80749858