shell脚本编程--正则表达式

shell脚本编程–正则表达式
上午知识点:
expect 预期交互
expect << EOF
spawn 执行的交互命令
expect “命令的提示关键字” { send “要发生的命令 \r”}

expect “#” { send “exit \r”}
EOF
变量的默认值
var =${var:-word}

正则表达式:
通配符 : * ? {} ^
*:匹配所有 *.txt
??.txt
[0-9] [a-z] [A-Z] [a-z] [0-9a-z] [1,2]
[^0-9]和[!0-9] //取反,非数字

  *5? :倒数第二位第5的
  {[abc],*.txt}: 匹配a,b,c 以.txt 结尾
   [abc]*.txt  :a或者b或者c开头后边任意以. txt结尾的

基本正则列表:
^ 匹配行首
$ 匹配行首
[] 集合,匹配集台中的任意单个字符
[^] 对集合取反
. 匹配任意单个字符
* 匹配*前一个字符任意次数[*不允许单独使用]
{n,m} 匹配前一个字符n到m次
{n} 匹配前一个字符n次
{n,} 匹配前一个字符n次以上
() 保留

扩展正则列表:
+ 最少匹配一次 1-∞
? 最多匹配一次 0-1 包含没有的情况
{n,m} 匹配n到m次
() 组合为整体,保留
| 或者
\b 单词边界

实例:
grep查询匹配的行:
egrep检索文本行:
echo -e 输出颜色

测试文件: /etc/passwd
1.输出以"r"开头的行

grep ‘^r’ /etc/passwd

2.输出以nologin结尾的行

grep ‘nologin$’ /etc/passwd

grep ‘/bin/bash$’ /etc/passwd

3.组合多个条件,查找以root开头的行或者以xieqc开头的行
#grep -e ‘root|xieqc’ /etc/passwd
#egrep ‘root|xieqc’ /etc/passwd
#grep ‘^root| ^xieqc’ /etc/passwd

grep 中的选项
-q :表示静默,只检索不输出
grep -q ‘^ root|^xieqc’ /etc/passwd && echo YES|I echo NO
-c :可输出匹配到的行数

grep -c ‘root|xieqc’ /etc/passwd

4.输出/etc/passwd中非空行
#egrep ‘.’ /etc/passwd
#egrep ‘^KaTeX parse error: Expected 'EOF', got '#' at position 24: …asswd 匹配空行 #̲egrep -v '^’ /etc/passwd

测试文件:/etc/rc.local
5.输出包括f,ff,fff…的行,数出f至少出现一次
#egrep ‘f+’ /etc/rc.local

6.输出包含init,initial的行
# egrep ‘init(ial)?’ /etc/rc.local

输出包含ab、abc、 abd、 abr、 aby、abi的行
# egrep ab[c,d,r,y,i]? /etc/rc.local

7.输出包含stu,stuff,…的行
# egrep ‘stuf*’ /etc/rc.local

8.输出所有行 .*  包含空行
 # egrep '.*' /etc/rc.local

9.输出以r开头,并且以nologin结尾的行,中间可以是任意字符
# egrep '^r.*nologin$' /etc/passwd

10. {} 输出包括abababab的行,即“ab”连续出现4次 
'(ab){4}'

11.输出包括abc、abd的行,
 'ab[cd]' 

 12.输出包括以hh结尾的单词的行
  'hh\b' 或者 'hh\>'  '\hh\>' '\hh\b'
    niha ma asdhh aha wohenhao
 
 匹配MAC地址:

以冒号分割一共6位。是一个16进制:00:50:C0:C5:R2C5
‘[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}’\

正则表达式练习作业:

cat regularexpress.txt

1.[root@cml ~]# egrep ‘the’ regularexpress.txt
2.[root@cml ~]# egrep -v ‘the’ regularexpress.txt
3.[root@cml ~]# egrep -i ‘the’ regularexpress.txt
4.[root@cml ~]# egrep ‘test|taste’ regularexpress.txt
5.[root@cml ~]# egrep ‘oo’ regularexpress.txt
6.[root@cml ~]# egrep -V ‘[^g]oo’ regularexpress.txt
7.[root@cml ~]# egrep ‘[^a-Z ]oo’ regularexpress.txt
8.[root@cml ~]# egrep -V ‘[0-9]’ regularexpress.txt
9.[root@cml ~]# egrep ‘^the’ regularexpress.txt
10.[root@cml ~]# egrep ‘1’ regularexpress.txt
11.[root@cml ~]# egrep -v ‘2’ regularexpress.txt
12.[root@cml ~]# egrep ‘.KaTeX parse error: Expected 'EOF', got '#' at position 37: …13.[root@cml ~]#̲ egrep '^’ regularexpress.txt
14.[root@cml ~]# egrep ‘g…d’ regularexpress.txt
15.[root@cml ~]# egrep ‘oo+’ regularexpress.txt
16.[root@cml ~]# grep ‘go{2,}g’ regularexpress.txt
17.[root@cml ~]# egrep -v ‘[0-9]’ regularexpress.txt
18.[root@cml ~]# grep ‘o{2}’ regularexpress.txt
19.[root@cml ~]# grep ‘go{2,5}g’ regularexpress.txt
20.[root@cml ~]# grep ‘go{2,}’ regularexpress.txt


  1. a-z ↩︎

  2. a-zA-Z ↩︎

发布了138 篇原创文章 · 获赞 149 · 访问量 8446

猜你喜欢

转载自blog.csdn.net/weixin_44799645/article/details/104898815