Solved

Need help with grep command

Posted on 2013-06-28
17
403 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now