Jump to content

You should learn regular expressions. Trying commands blindly will get you nowhere.

grep "^[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$"

so as you can see I'm matching a string that has 3 characters ("\{3\}") from 0 to 9 ("[0-9]") at the beginning of the line ("^")聽 followed by a hyphen ("\-"), another 3 characters from 0 to 9, another hyphen, 4 characters from 0-9 and the end of the line ("$").

image.png.cf03df3c7412f0e9b3055774f1c50d4d.png

Don't ask to ask, just ask... please聽馃え

sudo chmod -R 000 /*

Link to post
Share on other sites

This work for me on Linux:

grep -P "\d{3}-\d{3}-\d{4}" file.txt

-P = Use perl regular expressions

\d = Matches any digit (0-9)

\d{n} = Matches n digits in a row

So "\d{3}-\d{3}-\d{4}" matches three digits followed by a dash, three digits followed by a dash and four digits.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

4 minutes ago, Eigenvektor said:

This work for me on Linux:


grep -P "\d{3}-\d{3}-\d{4}" file.txt

-P = Use perl regular expressions

\d = Matches any digit (0-9)

\d{n} = Matches n digits in a row

So "\d{3}-\d{3}-\d{4}" matches three digits followed by a dash, three digits followed by a dash and four digits.

This doesn't quite work, you forgot to specify where the line begins and ends. Also I would assume perl regex isn't allowed in a grep exercise.

image.png.fff203cb63ae6feb3f0ceddd52210204.png

Don't ask to ask, just ask... please聽馃え

sudo chmod -R 000 /*

Link to post
Share on other sites

13 minutes ago, Sauron said:

This doesn't quite work, you forgot to specify where the line begins and ends. Also I would assume perl regex isn't allowed in a grep exercise.

He said he wants lines that "contain" the phone number, not just lines that consist of the phone number. My expression will return any line that has a phone number with the specified format in it, no matter where it occurs in that line.

Why would it not be allowed? If I'm supposed to use grep and grep offers this functionality, I consider it a valid solution :P

~edit: I will admit that my solution will also match a phone number like "2222-222-2222", so you could add the restriction that it has to either be surrounded by whitespace or must be at the start or end of the line:

"(^|\W)\d{3}-\d{3}-\d{4}($|\W)"

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

$ cat testfile.txt
Lorem ipsum dolor sit amet.Lorem ipsum dolor 222-222-2222 sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
duo dolores et ea rebum. Stet clita kasd 2222-222-2222 gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet,
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua. At 2222-222-22222 vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod

No matches:

grep "^[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$" testfile.txt

Yay, a match:

$ grep -P "(^|\W)\d{3}-\d{3}-\d{4}($|\W)" testfile.txt
Lorem ipsum dolor sit amet.Lorem ipsum dolor 222-222-2222 sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt

If you want the non-Perl version of that:

grep "\(^\|\W\)[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}\($\|\W\)" testfile.txt

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

1 hour ago, Eigenvektor said:

$ cat testfile.txt
Lorem ipsum dolor sit amet.Lorem ipsum dolor 222-222-2222 sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
duo dolores et ea rebum. Stet clita kasd 2222-222-2222 gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet,
sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua. At 2222-222-22222 vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod

No matches:


grep "^[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$" testfile.txt

Yay, a match:


$ grep -P "(^|\W)\d{3}-\d{3}-\d{4}($|\W)" testfile.txt
Lorem ipsum dolor sit amet.Lorem ipsum dolor 222-222-2222 sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt

If you want the non-Perl version of that:


grep "\(^\|\W\)[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}\($\|\W\)" testfile.txt

Hmm, I suppose that's right - I figured you'd want lines that contain only the phone number but I guess the question doesn't specify that.

But back the the perl thing, if they wanted him to use perl they'd just have said perl - at least, if I had given him this exercise I'd expect him to use grep syntax. Not that it changes much of course, it's just a matter of using [0-9] over \d.

Don't ask to ask, just ask... please聽馃え

sudo chmod -R 000 /*

Link to post
Share on other sites

4 minutes ago, Sauron said:

But back the the perl thing, if they wanted him to use perl they'd just have said perl - at least, if I had given him this exercise I'd expect him to use grep syntax. Not that it changes much of course, it's just a matter of using [0-9] over \d.

Yeah, I see your point. Guess it depends on how the teacher sees it. Personally, I'd focus more on teaching regex and less on a specific tool, but what can you do :) As you said, ultimately it's slightly different syntax.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

On 10/5/2019 at 7:28 AM, Sauron said:

You should learn regular expressions. Trying commands blindly will get you nowhere.


grep "^[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}$"

so as you can see I'm matching a string that has 3 characters ("\{3\}") from 0 to 9 ("[0-9]") at the beginning of the line ("^")聽 followed by a hyphen ("\-"), another 3 characters from 0 to 9, another hyphen, 4 characters from 0-9 and the end of the line ("$").

image.png.cf03df3c7412f0e9b3055774f1c50d4d.png

just an fyi, the commands weren't being tried blindly, the purpose is the learn regular expressions but its online class so asking the professor is 3am or whatever isnt always an option. either way i managed to work it out with another student.

Link to post
Share on other sites

On 10/8/2019 at 2:29 PM, Dat Guy said:

Which is exactly where simply calling Perl would be much more efficient.

Unless you're parsing through terabytes of text, it's probably not going to make a measurable difference.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now