-1

I'm just trying to look for all lines that have this number format: "###-##-####" kind of like a social security number. An example:

"987-65-4321, Hadfield, Chris" is one line in the file, all lines should follow this similar format. I want to validate that the format is correct.

I'm having problems as I'm not experienced with regex. I've tried patterns that I believed would work but wouldn't

Currently I'm at grep "^[0-9]{3}\-[0-9]{2}\-[0-9]{4}" which actually won't return any results. So it's NOT working for me.

Also I'm not sure why but even when I try to use "\d" even by itself, it will only return strings containing a literal "d" and not a digit.

Emma
  • 27,428
  • 11
  • 44
  • 69
Artish1
  • 113
  • 1
  • 12
  • [SSN Regex for 123-45-6789 OR XXX-XX-XXXX](https://stackoverflow.com/q/4087468/608639), [Regular Expression for SSN](https://stackoverflow.com/q/7067874/608639), etc. Phone numbers are similar and may be more popular: [A comprehensive regex for phone number validation](https://stackoverflow.com/q/123559/608639). Also see [Social Security Number | Valid SSNs](https://en.wikipedia.org/wiki/Social_Security_number#Valid_SSNs). – jww Apr 29 '19 at 01:02
  • @jww The link for "Regular Expression for SSN" is in essence, the exact same regex that I have, just that ```\d``` is replaced with ```[0-9]``` range. Also for some reason ```\d``` won't work reliably for me – Artish1 Apr 29 '19 at 01:08
  • What about: `grep -P '\d{3}\-\d{2}\-\d{4}' file`? – Paul T. Apr 29 '19 at 01:12
  • @PaulT. That actually works. Thank you! – Artish1 Apr 29 '19 at 01:16
  • 1
    You needed `grep -E` or `egrep` for the RE in your question, btw. – Shawn Apr 29 '19 at 01:22
  • @Artish - Please place answers in Answer blocks. Later, you can accept your own Answer. Also see [How does accepting an answer work?](https://meta.stackexchange.com/q/5234/173448) – jww Apr 29 '19 at 01:24

1 Answers1

0

grep -P or grep -E in order to factor in regular expressions.

Artish1
  • 113
  • 1
  • 12