簡體   English   中英

使用“grep”命令查找包含電話號碼的txt文件

[英]use “grep” command to find txt files containing telephone number

使用“grep”命令查找包含電話號碼的txt文件。

電話號碼格式可以是:

"***-*******"
"**********"
"*** *******"
"***-***-****"

怎么寫呢?

數字是3位數(\\ d {3}),空格或短划線[\\ s-],3位數(\\ d {3}),空格或短划線[\\ s-],4位數(\\ d {4})。

grep '\d{3}[\s\-]{0,1}\d{3}[\s\-]{0,1}\d{4}' infile.txt

編輯 @Mark和Jed - 好建議 - 根據建議進行更新

$ cat file
test 1234567890 test
blah 123-4567890 junk junk 546-3345622 junk
blah blah 123 4575463 sdfsljads 123_456_4356 end
123--4567890

$ grep -Po '\d{3}[\s\-_]?\d{3}[\s\-_]?\d{4}'  file
1234567890
123-4567890
546-3345622
123 4575463
123_456_4356

$ ruby -ne 'puts $_.scan(/\d{3}[\s\-_]?\d{3}[\s\-_]?\d{4}/)' file
1234567890
123-4567890
546-3345622
123 4575463
123_456_4356

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM