Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with grep command

Posted on 2013-06-28
17
Medium Priority
?
414 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
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 500 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 85

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

971 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