Solved

Need help with grep command

Posted on 2013-06-28
17
411 Views
Last Modified: 2013-06-28
I am trying to capture lines that start with / followed by one letter (any letter) and then numbers

This:

grep "^\/[a-z]"

worked well retrieving lines starting with / and an alphanumeric character but changing it to

grep "^\/[a-z][0-9]"

and

grep "^\/[a-z][0123456789]"


did not return anything.

Can someone help?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 39284836
Try:

grep '^\/[a-zA-Z0-9]
0
 
LVL 35

Author Comment

by:YZlat
ID: 39284842
I have tried '^\/[a-z0-9]'  didn't work
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284844
pony10us - that will match '/8' which isn't what the author asked.

This one:

grep "^\/[a-z][0-9]"

Should have worked.

Can you post a couple sample lines that should match from your file?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Author Comment

by:YZlat
ID: 39284854
/a12
/test

my code should only return /a12 but with '^\/[a-z0-9]' it returns both /a12 and /test
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284855
Here's my sample file (named 'foo')
hey
/hey
/h0ey
blah
/blah
/b5lah!

Open in new window

My command:
grep "^\/[a-z][0-9]" foo

Open in new window

And my output:
/h0ey
/b5lah!

Open in new window

Looks correct to me.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284858
Yeah, you don't want that one.  You want this:

grep "^\/[a-z][0-9]"

Which you originally posted, but said it wasn't returning anything.  But that is what you want.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39284874
Something got messed up (probably my fingers :) )

Should have been:

grep "^\/[a-zA-Z0-9]"

nemws1: - yours should return anything that starts with "/" and 2 more characters.

EDIT:  Nevermind.  I got interrupted and posted before seeing the rest of the replies.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284891
pony10us - yours will still match this:

/1

Which isn't what the asker wants.  "/", then alpha char, *then* number:
trying to capture lines that start with / followed by one letter (any letter) and then numbers

EDITED: Ha!  I am so guilty of doing the same thing.  No worries pony10us.  :)
0
 
LVL 35

Author Comment

by:YZlat
ID: 39284896
my bad, line does not always begin with slash alphanumeric, in most cases it ends with it:

/string           temp         server1         /a12
value              exec         server2         /rtest
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39284905
You are correct.  I read the part that said it didn't return anything and skipped right over "followed by one letter (any letter) and then numbers.  I was focusing on it being:

/ followed by any letter "OR" number

Yours should be the correct code.

I hate getting interrupted by work when trying to be helpful on EE.   ROFL
0
 
LVL 23

Accepted Solution

by:
nemws1 earned 125 total points
ID: 39284908
Ah.  Is it either/or?  Ie - it either *starts* with /a12 or *ends* with /a12, but you don't care if "/a12" is in the middle?

If you want lines that just contain "/a12" (or similar), just get rid of the caret (^):
grep "\/[a-z][0-9]" foo

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284914
If it has to be either start/end and might appear in the middle (and you don't want these), you'll need to use 'egrep' instead of 'grep' to do it in one command:

egrep '(^\/[a-z][0-9])|(\/[a-z][0-9]+)$' foo

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39284921
Do you want a grep command that matches
/string           temp         server1         /a12
or
value              exec         server2         /rtest
?
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39284938
One more observation.  the examples all show lower case however if there is a possiblity of upper case then you may still want to change the [a-z] to be a-zA-Z]
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39284945
Quite true, pony10us.

Or just add '-i' to the e?grep command.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39284965
Think I might need a new keyboard or stronger fingers?  

a-zA-Z]

should be [a-zA-Z]
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 39285194
Worked! Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question