LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2018, 06:50 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
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.
 
Old 06-29-2018, 07:12 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.
 
Old 06-29-2018, 07:36 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,345

Rep: Reputation: Disabled
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.
 
1 members found this post helpful.
Old 06-29-2018, 08:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Quote:
Originally Posted by abefroman View Post
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 ?.
 
Old 06-29-2018, 08:11 PM   #5
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by syg00 View Post
So, what's the problem ?.
I mean there will never be any records I'm interested in with 5,6, or 7
 
Old 06-29-2018, 09:06 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by abefroman View Post
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!!

Last edited by scasey; 06-29-2018 at 09:08 PM.
 
Old 06-29-2018, 09:07 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
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.
 
Old 06-30-2018, 10:18 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
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
 
1 members found this post helpful.
Old 07-01-2018, 07:53 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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]|$)'
 
Old 07-04-2018, 08:33 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
[SOLVED] grep for a string in nested string threezerous Linux - Newbie 3 09-12-2014 10:04 AM
[SOLVED] Issue trying to grep a string, but keep a similar string Supp0rtLinux Linux - Software 7 10-04-2011 06:35 PM
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
escapeing the characters '[' and ']' in grep logicalfuzz Linux - Newbie 3 03-21-2006 03:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration