shell脚本之——正则表达式的应用(案例分析)以及函数的应用(案例分析)

1、使用正则表达式,获取指定的座机号

以下座机号码:

025 555555555555
025 56855528
025-86666666
(025) 86656666
025 12123333
025CDD66666
02566666666

从上面号码中过滤出下面这些号码,使其所有数字都变红

025 56855528
025-86666666
(025) 86656666
02566666666

1.1 格式

[root@weq ~]# cat testfile5
025 555555555555
025 56855528
025-86666666
(025) 86656666
025 12123333
025CDD66666
02566666666
[root@weq ~]# egrep "^\(*025\)*[ -]*[586][0-9]{7}$" testfile5
025 56855528
025-86666666
(025) 86656666
02566666666
[root@weq ~]# egrep "^\(?025\)?[ -]?[586][0-9]{7}$" testfile5
025 56855528
025-86666666
(025) 86656666
02566666666

1.2 输出结果

  

2、使用正则表达式,获取指定的邮箱

要求:

用户名@:字符长度在6位及以上,开头只能是字母或者_,中间可使用的符号有:. - # _

子域名:可能是大小写字母、数字,_ - .

.顶级域名:字符串长度一般在2-5之间

2.1 格式

[root@weq ~]# cat aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn
li li@163.com
qian@qi@wo.cn
www 13@qq.com
[root@weq ~]# egrep "^([a-zA-Z_][a-zA-Z0-9_#\.\-]{5,})@([a-zA-Z0-9_\.\-]+)\.([a-zA-Z]{2,5})" aaa.txt 
zhangsan1234@qq.com
lucy_789@sina.com.cn  

2.2 输出结果 

3、请通过在命令行中执行./output 20 30 输出20+30的值,脚本中使用sum()函数封装代码并通过调用sum函数返回结果。

3.1  格式

#!/bin/bash
#返回值

sum () {
     sum1=$[a + b]
     echo $sum1
}
a=$1
b=$2
sum  

3.2 输出结果 

 

posted on 2021-07-28 11:18  笑洋仟  阅读(170)  评论(0编辑  收藏  举报

levels of contents