user3834663
user3834663

Reputation: 543

Regex to match numeric pattern

I am trying to match specific numeric pattern from the below list. My requirement is to match only report.20150325 to report.20150331. Please help.

report.20150319
report.20150320
report.20150321
report.20150322
report.20150323
report.20150324
report.20150325
report.20150326
report.20150327
report.20150328
report.20150329
report.20150330
report.20150331

Upvotes: 0

Views: 91

Answers (4)

Avinash Raj
Avinash Raj

Reputation: 174706

Seems like you want to print the lines which falls between the lines which matches two separate patterns (including the lines which matches the patterns).

$ sed -n '/^report\.20150325$/,/^report\.20150331$/p' file
report.20150325
report.20150326
report.20150327
report.20150328
report.20150329
report.20150330
report.20150331

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203483

A regexp match isn't always the right approach. Here you are asking to match a string followed by a number so use a string and numeric comparisons:

$ awk -F'.' '$1=="report" && ($2>=20150325) && ($2<=20150331)' file
report.20150325
report.20150326
report.20150327
report.20150328
report.20150329
report.20150330
report.20150331

Upvotes: 0

npinti
npinti

Reputation: 52185

You could use something like so: ^report\.201503(2[5-9]|3[01])$/gm (built using this tool).

It should match the reports you are after, as shown here.

Upvotes: 0

Braj
Braj

Reputation: 46841

It's very simple to match 25 to 31 use regex 2[5-9]|3[01]

Here is complete regex

(report\.201503(2[5-9]|3[01]))

DEMO

Explanation of 2[5-9]|3[01]

2 followed by a single character in the range between 5 and 9
OR
3 followed by 0 or 1

Upvotes: 1

Related Questions