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}" |
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. |
As for matching either pattern X or Y, use (X|Y):
Code:
grep -E "name-([0-9]{4}|[0-9]{8})" Code:
grep -E "name-([0-9]{4}|[0-9]{8})$" Code:
grep -E "name-([0-9]{4}|[0-9]{8})[^0-9]" |
Quote:
Quote:
|
Quote:
|
Quote:
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})$" 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!! |
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. |
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 |
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]|$)' |
Possibly
Code:
grep -E "name-[0-9]+" Quote:
|
All times are GMT -5. The time now is 09:05 PM. |