1

I need to create a script that will search for US phone numbers in files that are in a directory that has been passed in as a parameter to your script. The script must recognize phone numbers in the following formats: (570)555-1212, 570.555.1212, 570-555-1212, and +1.570.555.1212. My script should also be able attempt to minimize false positives. The script should work on files that are compressed or uncompressed.

The output should be similar to the following.

letter.docx: (312)555-1212 570.389.3000

intro.txt: 570-389-3000

I have no idea where to start other than

#!/usr/bin/bash
zgrep -e 1 -q '[0-9]{3}-[0-9]{3}-[0-9]{4}','[0-9]{3}.[0-9]{3}.[0-9]{4}','+1.[0-9]{3}.[0-9]{3}.[0-9]{4}' 
image.dd
if [ $? -eq 0 ] ; then echo matches ; else echo "no match found" ; fi
tink
  • 14,342
  • 4
  • 46
  • 50
Riderine
  • 11
  • 2
  • 2
    You've chosen the most difficult way possible of solving this problem: writing a complete script and then trying to debug all its problems simultaneously. You will save yourself a ton of trouble if you start small and test each addition thoroughly before moving on to the next one. In this case, you first attempt should have been something like `grep test` to figure out why that's not working before you even try to get phone numbers involved – that other guy Feb 25 '19 at 20:57
  • Thank you, I will scale down and try again but I must use zgrep. – Riderine Feb 25 '19 at 21:00
  • You don't even need `grep` yet, because the first problem is "How do I access a directory passed in as a parameter?" Again I really recommend not making any changes until they're needed and you can test them thoroughly. If you use `zgrep` now because you need to search compressed files, I think you'll be stuck later trying to debug why your regex doesn't match anything in `letter.docx`. If you instead use `grep` until you have to deal with compressed files, you'll quickly discover that `zgrep` doesn't work on `docx` files because `zgrep` only supports gzip compression while `docx` uses zip – that other guy Feb 25 '19 at 21:37

0 Answers0