Solved

Need help with grep command

Posted on 2013-06-28
17
412 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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 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…
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…
Suggested Courses

628 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