Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-10-2010
Registered User
 

Join Date: Feb 2010
Posts: 60
Thanks: 15
Thanked 0 Times in 0 Posts
Thumbs up Help with grep / regular expression

Hi,

Input file:

Code:
-13-
-1er-
-1xyz1-
-1xz12-
-2ab1-
-2ab2--
-143-

Code:

Code:
grep '^[\-][0-9][a-z]*[0-9]\-' input.txt

Wrong output:

Code:
-13-
-1xyz1-
-2ab1-
-2ab2--

Expected output:

Code:
-1xyz1-
-2ab1-

This is what i am trying to do:
1)starts with - as first one
2) second character any number
3) third one any letter a to z
4) penultimate char - any number
5) last one is with -

Thanks in advance .
Sponsored Links
    #2  
Old 09-10-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
Hi.

Your description could be considered slightly ambiguous, but:


Code:
$ egrep '^-[0-9][a-z]+[0-9]-$' file1
-1xyz1-
-2ab1-

The Following User Says Thank You to Scott For This Useful Post:
dragon.1431 (09-10-2010)
Sponsored Links
    #3  
Old 09-10-2010
Registered User
 

Join Date: Feb 2010
Posts: 60
Thanks: 15
Thanked 0 Times in 0 Posts
Hi Scottn,
just curious, is it not possible to do it with grep / awk ?
    #4  
Old 09-10-2010
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 

Join Date: Sep 2007
Location: Germany
Posts: 5,193
Thanks: 47
Thanked 280 Times in 267 Posts
He is using grep, ie. egrep is like grep -E.
Sponsored Links
    #5  
Old 09-10-2010
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,352
Thanks: 130
Thanked 535 Times in 473 Posts
And the awk is the same:


Code:
$ awk '/^-[0-9][a-z]+[0-9]-$/' file1
-1xyz1-
-2ab1-

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regular expression at the end of grep iga3725 Shell Programming and Scripting 3 06-11-2010 09:46 AM
grep and regular expression kminkeller Shell Programming and Scripting 9 02-24-2010 12:55 AM
grep with regular expression daikeyang Shell Programming and Scripting 6 03-22-2009 07:46 PM
regarding grep regular expression ukatru UNIX for Advanced & Expert Users 2 10-22-2008 11:47 PM
grep : regular expression RishiPahuja Shell Programming and Scripting 10 09-13-2005 12:53 AM



All times are GMT -4. The time now is 04:17 AM.