Thursday, February 8, 2024

[SOLVED] Match string and one or more numbers with grep

Issue

I've got a file myfile. I want to find lines that includes following pattern:

MyWebsite/1234

or more general

somecharactersstringswhitespacesMyWebsite/987somecharactersstringswhitespaces

There can be 1 or more numbers after MyWebsite. I have tried following:

grep ".*MyWebsite[0-9]\{1,\}" myfile
grep "MyWebsite[0-9]\{1,\}" myfile
grep MyWebsite"[0-9]\{1,\}" myfile
grep -E ".*MyWebsite/[0-9]\{1,\}.*" myfile

None of them worked, even if intuition and logic says that should be good.


Solution

grep 'MyWebsite/[0-9]\+' myfile should work. On your first 3 regexes, you missed the / and on the last, you shouldn't have escaped the { and }, but otherwise you were pretty good. It's more concise to use + rather than {1,} though, and the version of regex that grep uses by default (a variant of POSIX BRE) requires it to be escaped.



Answered By - squirl
Answer Checked By - Mildred Charles (WPSolving Admin)