LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I grep x or y characters after string? (https://www.linuxquestions.org/questions/programming-9/how-can-i-grep-x-or-y-characters-after-string-4175632969/)

abefroman 06-29-2018 06:50 PM

How can I grep x or y characters after string?
 
How can I grep x or y characters after string?

Ex. name-1234 or name-12345678 (there will always be 4 or 8 numbers, never 5,6 or 7)

I have:
Code:

grep -E "name-[0-9]{4}"
If I do {4,8} that matches 4-8 characters, rather than 4 or 8, which I'm trying to do.

scasey 06-29-2018 07:12 PM

I note that your code already matches both, since name-12345678 contains name-1234.
Given there are never 5,6 or 7, doesn't this fulfill your needs? grep, after all is only going to select/display matching lines.

Ser Olmy 06-29-2018 07:36 PM

As for matching either pattern X or Y, use (X|Y):
Code:

grep -E "name-([0-9]{4}|[0-9]{8})"
Now, keep in mind that [0-9]{4} will indeed match 12345 or 123456 or indeed 1234whatever, since you haven't done anything to restrict the match based on what might follow the pattern specified. If the pattern is supposed to be followed by EOL, then this should do:
Code:

grep -E "name-([0-9]{4}|[0-9]{8})$"
If the pattern may be followed by anything except a digit, this will do the trick:
Code:

grep -E "name-([0-9]{4}|[0-9]{8})[^0-9]"
...and so on.

syg00 06-29-2018 08:04 PM

Quote:

Originally Posted by abefroman (Post 5873716)
If I do {4,8} that matches 4-8 characters, rather than 4 or 8, which I'm trying to do.

Quote:

(there will always be 4 or 8 numbers, never 5,6 or 7)
So, what's the problem ?.

abefroman 06-29-2018 08:11 PM

Quote:

Originally Posted by syg00 (Post 5873734)
So, what's the problem ?.

I mean there will never be any records I'm interested in with 5,6, or 7

scasey 06-29-2018 09:06 PM

Quote:

Originally Posted by abefroman (Post 5873739)
I mean there will never be any records I'm interested in with 5,6, or 7

(emphasis added)
Oh...that's much different than: there will never be any records with 5,6, or 7

I've just verified that Ser Olmy's
Code:

grep -E "name-([0-9]{4}|[0-9]{8})$"
works just dandy!

I'm happy to have learned stuff from you (I didn't completely grok quantifiers before) and from Ser Olmy's example of how to use OR constructs.

regexp: learn something new every day!!

syg00 06-29-2018 09:07 PM

You (and we) can't write the regex until you fully define the data. Precisely.
That said, it's looking like perlre using look-arounds might be the go.

danielbmartin 06-30-2018 10:18 AM

Help us to help you. Create a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. To preserve formatting, highlight your samples and click on the # in the row immediately above the Reply window.

With "InFile" and "OutFile" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin

MadeInGermany 07-01-2018 07:53 AM

You can even allow trailing characters; the immediately following character is not a number or there is the end of the line.
Code:

grep -E 'name-([0-9]{4}|[0-9]{8})([^0-9]|$)'

chrism01 07-04-2018 08:33 PM

Possibly
Code:

grep -E "name-[0-9]+"

name-12345678
name-1234

Quote:

there will always be 4 or 8 numbers, never 5,6 or 7
but what about 1, 2 or 3 digits ?


All times are GMT -5. The time now is 09:05 PM.