標籤:shell 三劍客 grep sed awk
【問題描述】
會員提供的資訊中,有些手機號是會員隨意輸入的,因此要統計出有效手機號。
如以下資訊:
1829508936818950893681850893681820893681788888888817884432254178881324351781226668818295089368182350893681333550838715575089368
【解決辦法】
利用grep,sed與awk結合正則即可。下面分別說明這三個的用法。
1.grep
[email protected] test$egrep ‘^1[3578][0-9]{9}‘ test.txt 182950893681788888888818295089368182350893681333550838715575089368[email protected] test$grep -oP ‘(?<=‘‘)(1[3578]{1}[0-9]{9})(?=‘‘)‘ test.txt182950893681788888888818295089368182350893681333550838715575089368
2.sed
[email protected] test$sed -n ‘/1[3578]\{1\}[0-9]\{9\}/p‘ test.txt182950893681788888888818295089368182350893681333550838715575089368[email protected] test$sed -rn ‘/1[3578]{1}[0-9]{9}/p‘ test.txt182950893681788888888818295089368182350893681333550838715575089368
3.awk
[email protected] test$awk --posix ‘/1[3578]{1}[0-9]{9}/‘ test.txt182950893681788888888818295089368182350893681333550838715575089368
注;以上的awk中的--posix啟用後就支援間隔運算式了,即r{n},r{n,},r{n,m}
然後說一下個人化需求。
1.如尋找含有連續兩個8的手機號。
[email protected] test$grep -E ‘1[3578]{1}.*[8]{2}.*‘ test.txt 17888888888178844322541788813243517812266688[email protected] test$sed -rn ‘/1[3578]{1}.*[8]{2}.*/p‘ test.txt17888888888178844322541788813243517812266688[email protected] test$awk --posix ‘/1[3578]{1}.*[8]{2}.*/‘ test.txt17888888888178844322541788813243517812266688
2.尋找末尾是兩個8的手機號
[email protected] test$grep -E ‘1[3578]{1}[0-9]{7}[8]{2}‘ test.txt 1788888888817812266688[email protected] test$sed -rn ‘/1[3578]{1}[0-9]{7}[8]{2}/p‘ test.txt1788888888817812266688[email protected] test$awk --posix ‘/1[3578]{1}[0-9]{7}[8]{2}/‘ test.txt1788888888817812266688
Linux三劍客學習之提取手機號碼