Solved

Need help with grep command

Posted on 2013-06-28
17
405 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
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 how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

895 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