shell正則式解析身份證和手機號


 

cat test2.html | sed -e 's/\(^\|[^0-9]\)\(13[0-9][0-9]\{8\}\|14[579][0-9]\{8\}\|15[0-3,5-9][0-9]\{8\}\|16[6][0-9]\{8\}\|17[0135678][0-9]\{8\}\|18[0-9][0-9]\{8\}\|19[89][0-9]\{8\}\)\($\|[^0-9]\)/\nfind_phone:\2\n/g' | sed -e 's/\(^\|[^0-9]\)\([0-9]\{6\}[1-2][0-9]\{3\}\(\(0[1-9]\)\|\(10\|11\|12\)\)\(\([0-2][1-9]\)\|10\|20\|30\|31\)[0-9]\{3\}[0-9Xx]\)\($\|[^0-9]\)/\nfind_idcard:\2\n/g' | awk '/find_.*/{printf $1;printf "\t"}' 


測試文件test2.html內容:

dddd
bbb131102198910084421ccc eee13611112222fff13133334444
h15855556666j
aaaa
 13177778888
13199990000
 18611112222
370785199507319527


測試結果:

find_idcard:131102198910084421    find_phone:13611112222    find_phone:13133334444    find_phone:15855556666    find_phone:13177778888    find_phone:13199990000    find_phone:18611112222    find_idcard:370785199507319527    

 


 身份證號正則式:https://www.jb51.net/article/109384.htm

只是參考,不能直接用,shell中或|要加\;左右括號()也要加\;表示8個數字應為[0-9]\{8\} https://zhidao.baidu.com/question/1115861792946350259.html;^表示開頭$表示結尾,不需要加\

 

手機號正則式:https://blog.csdn.net/voidmain_123/article/details/78962164 同只是參考,不能直接用

 

awk命令:按行讀取。未匹配上的不保留 https://www.cnblogs.com/xudong-bupt/p/3721210.html

sed命令:我自己試出來的。。

 

awk、sed、grep、fgrep、egrep:

https://www.cnblogs.com/EasonJim/p/8282511.html

 https://blog.csdn.net/qq504196282/article/details/52995198

https://www.cnblogs.com/moveofgod/p/3540575.html

同時匹配ABC 和 123:
sed -n '/ABC/{/123/p}'

awk '/ABC/&&/123/{ print $0 }'

grep -E '(ABC.*123|123.*ABC)'

匹配ABC 或 123:
sed -n '/\(ABC\|123\)/p'

awk '/ABC/||/123/{ print $0 }'

grep -E '(ABC|123)'egrep 'ABC|123'

 

 

 

shell awk輸出換行print,shell輸出不換行printf,連續輸出中間用分號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM