Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex help

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
How can I test for the following:
Code:
A4ABCdd
a654321
a987463
1122322

I want to get first character be [a-z] and the last 6 characters be numbers [0-9]

So, this would print out
Desired output:
Code:
a654321
a987463
 
egrep '^[a-z][0-9]{6}$'

see man egrep


HTH,

p5wizard
 
Note that unfortunately some egrep's don't support the {#repeats} syntax, for e.g. on Solaris you need to use /usr/xpg4/bin/egrep.

Annihilannic.
 
Well it works on AIX, and most of Mag's questions are AIX-related so...

And as this is an AWK forum, I tested it... and it works in awk on AIX also.


HTH,

p5wizard
 
Code:
awk '{ if ($1==/^[a-z][0-9]{6}$/) print $1 }' /tmp/file

does that look right?
 
Try this instead:
awk '$1~/^[a-z][0-9]{6}$/{print $1}' /tmp/file

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Could you please post your actual code and sample records of /tmp/file ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sure

Code:
cat /tmp/file
A4ABCdd
a654321
a987463
1122322

I am trying to get the following output
Code:
a654321
a987463

I want to get first character be [a-z] and the last 6 characters be numbers [0-9]

Trying something like this
Code:
awk '{ if ($1==/^[a-z][0-9]{6}$/) print $1 }' /tmp/file

TIA
 
Have you even tried the code I suggested you 25 Oct 06 14:19 ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
with that input file even this awk code should work:

awk '/^[a-z][0-9]{6}$/' /tmp/file


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top