LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2011, 08:19 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
grep with pattern


Input: each line in the file is six characters.

Desired output: all lines which have "as" or "ek" in positions 3 and 4.

grep '..[as|ek]..' doesn't do the trick.

grep '..[a|e][s|k]..' also fails.

Please advise.

Daniel B. Martin
 
Old 12-05-2011, 08:24 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
The problem are the square brackets. You're defining a character
class, overthinking the pattern.

Code:
egrep '^..(as|ek)..'
should do.
 
Old 12-05-2011, 09:13 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Original Poster
Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Excellent. Thank you!

egrep '^..(as|ek)..'
This is the answer.

Would egrep '^..(as|ek)'
also be correct?

Daniel B. Martin
 
Old 12-05-2011, 10:16 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by danielbmartin View Post
Excellent. Thank you!

egrep '^..(as|ek)..'
This is the answer.

Would egrep '^..(as|ek)'
also be correct?

Daniel B. Martin

If the file-format is well described, and all lines do indeed
have the six characters, and this is not an additional requirement
for you to check, yes, absolutely.


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
grep till second pattern rattlesnakejoe Programming 1 11-22-2009 08:09 AM
[SOLVED]matching a pattern with grep kdelover Programming 2 09-18-2009 06:07 AM
grep exact pattern kapila Linux - Newbie 11 08-06-2009 02:58 AM
grep lines between occurances of a pattern ksri07091983 Solaris / OpenSolaris 5 04-26-2009 06:14 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:29 PM.

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