Grep
0
无    2020-09-02 18:09:27    1    0
myron

grep用法:


grep -v "^#" >/etc/ntp.conf

Linux三剑客学习之提取手机号码

[摘要:【题目描绘】会员供应的疑息中,有些脚机号是会员随便输进的,是以要统计出有用的脚机号。如以下疑息:18295089368 1895089368 185089368 182089368 17888888888 17884432254 178]

【问题描述】

会员提供的信息中,有些手机号是会员随意输入的,因此要统计出有效的手机号。

如以下信息:

18295089368
1895089368
185089368
182089368
17888888888
17884432254
17888132435
17812266688
18295089368
18235089368
13335508387
15575089368
【解决办法】

利用grep,sed与awk结合正则即可。下面分别说明这三个的用法。

1.grep

root@Network test$egrep '^1[3578][0-9]{9}' test.txt 
18295089368
17888888888
18295089368
18235089368
13335508387
15575089368
root@network test$grep -oP '(?<='')(1[3578]{1}[0-9]{9})(?='')' test.txt
18295089368
17888888888
18295089368
18235089368
13335508387
15575089368​


2.sed

root@network test$sed -n '/1[3578]\{1\}[0-9]\{9\}/p' test.txt
18295089368
17888888888
18295089368
18235089368
13335508387
15575089368
root@network test$sed -rn '/1[3578]{1}[0-9]{9}/p' test.txt
18295089368
17888888888
18295089368
18235089368
13335508387
15575089368​


3.awk

root@oldboy test$awk --posix '/1[3578]{1}[0-9]{9}/' test.txt
18295089368
17888888888
18295089368
18235089368
13335508387
15575089368​


注;以上的awk中的--posix启用后就支持间隔表达式了,即r{n},r{n,},r{n,m}

然后说一下个性化需求。

1.如查找含有连续两个8的手机号。

root@network test$grep -E '1[3578]{1}.*[8]{2}.*' test.txt 
17888888888
17884432254
17888132435
17812266688
root@network test$sed -rn '/1[3578]{1}.*[8]{2}.*/p' test.txt
17888888888
17884432254
17888132435
17812266688
root@network test$awk --posix '/1[3578]{1}.*[8]{2}.*/' test.txt
17888888888
17884432254
17888132435
17812266688​


2.查找末尾是两个8的手机号

root@network test$grep -E '1[3578]{1}[0-9]{7}[8]{2}' test.txt 
17888888888
17812266688
root@network test$sed -rn '/1[3578]{1}[0-9]{7}[8]{2}/p' test.txt
17888888888
17812266688
root@network test$awk --posix '/1[3578]{1}[0-9]{7}[8]{2}/' test.txt
17888888888
17812266688​




__________________________________________________________

Bind DNS配置
文档导航