Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Less greedy...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 33 of 33 Topic 2223 of 2236
Post > Topic >>

Re: Less greedy pattern match

by Ed Morton <morton@[EMAIL PROTECTED] > Apr 16, 2008 at 11:48 PM

On 4/16/2008 5:01 PM, Rajan wrote:
> 
> "Ed Morton" <morton@[EMAIL PROTECTED]
> wrote in message 
> news:4805EBDC.8080700@[EMAIL PROTECTED]
> 
>>
>>On 4/15/2008 11:54 PM, Rajan wrote:
>>
>>>"Prateek" <prateek.a@[EMAIL PROTECTED]
> wrote in message
>>>news:9807e551-cdc4-4006-9b52-68d833d56787@[EMAIL PROTECTED]
>>>
>>>
>>>>On Apr 16, 12:02 am, "Rajan" <svra...@[EMAIL PROTECTED]
> wrote:
>>>>
>>>>
>>>>>"Ed Morton" <mor...@[EMAIL PROTECTED]
> wrote in message
>>>>>
>>>>>news:48057145.9090408@[EMAIL PROTECTED]
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>>On 4/15/2008 9:59 PM, Prateek wrote:
>>>>>>
>>>>>>
>>>>>>>Hi,
>>>>>>>This question is regarding the less greedy regular expression
pattern
>>>>>>>match. As you know, Perl provides less greedy pattern match
parameter
>>>>>>>in the form of repetition?
>>>>>>
>>>>>>>But does gawk provide anything similar?
>>>>>>
>>>>>>>The sample I was trying was:
>>>>>>
>>>>>>>echo ccccccd | gawk '{ print match ($0,c*?d)
>>>>>>
>>>>>>>I was expecting value 6 as output
>>>>>>
>>>>>>why?
>>>>>
>>>>>>>but was always getting value 1.
>>>>>>
>>>>>>>(The above is only an example; my patterns are more complex and was
>>>>>>>trying to find less greedy operators)
>>>>>>
>>>>>>There's no "less greedy" operator (to be honest, I'm not sure what
that
>>>>>>really
>>>>>>means or why it'd be more useful than a different RE), but if you
post
>>>>>>some
>>>>>>sample input and expected output we can probably help get what you
want
>>>>>>using
>>>>>>the existing functionality.
>>>>>
>>>>>>Ed.
>>>>>
>>>>>match returns the index of start of match within the string not
number 
>>>>>of
>>>>>occurrences .
>>>>>For less greedy expressions you can use interval expressions like
c{1}.
>>>>>If
>>>>>you are looking for number of consecutive occurrences change your
c*?d 
>>>>>to
>>>>>c*
>>>>>and print RLENGTH. If you are looking for occurrences that are
>>>>>non-consecutive use a gsub in combination with an interval
expression.
>>>>>
>>>>>Rajan
>>>>
>>>>Hi Rajan,
>>>>Can you please give an example of "if you are looking for occurrences
>>>>that are
>>>>non-consecutive use a gsub in combination with an interval
>>>>expression."
>>>>
>>>>Thanks,
>>>>Prateek
>>>
>>>
>>>Prateek, If I want to count all occurrences of "cici", I would do it
like
>>>below
>>>echo ccccccdcicidcicicicid | gawk  --re-interval '{tmp=$0; print
>>>gsub(/(ci){2}/,"anytext",tmp)}'
>>>Rajan
>>>
>>
>>I'd do:
>>
>>echo ccccccdcicidcicicicid | awk  -F'cici' '{print NF ? NF-1 : 0}'
>>
>>Ed.
>>
> 
> 
> Cool, that is better and easier. As long as you are resetting FS and
forcing 
> gawk to re-split fields before doing anything else.

If I needed to use a different FS for the "main" part of the script, then
I'd
instead do:

echo ccccccdcicidcicicicid | awk '{c=split($0,t,"cici"); print c ? c-1 :
0}'

I'd really like to see the OP post some real examples though - I still
don't
know what the real problem is he's trying to solve but I doubt if it's the
one
we're addressing!

	Ed.




 33 Posts in Topic:
Less greedy pattern match
Prateek <prateek.a@[EM  2008-04-15 19:59:12 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-15 22:23:49 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 00:02:06 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 20:32:10 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 21:12:47 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 14:33:44 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 22:01:46 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-17 22:12:04 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-17 19:20:05 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 23:13:31 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-17 23:36:59 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 09:50:29 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-18 09:27:00 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 19:05:23 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 19:24:27 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-18 22:06:17 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-18 21:19:26 
Re: Less greedy pattern match
Cesar Rabak <csrabak@[  2008-04-19 13:15:05 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-20 08:36:44 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-20 09:58:54 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-20 10:21:46 
Re: Less greedy pattern match
Janis Papanagnou <Jani  2008-04-19 18:53:19 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-20 14:32:54 
Re: Less greedy pattern match
Janis Papanagnou <Jani  2008-04-20 16:30:25 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-21 09:16:17 
Re: Less greedy pattern match
pk <pk@[EMAIL PROTECTE  2008-04-22 10:09:12 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-22 06:14:10 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-18 06:28:14 
Re: Less greedy pattern match
Prateek <prateek.a@[EM  2008-04-15 21:21:23 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 00:54:18 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-16 07:06:52 
Re: Less greedy pattern match
"Rajan" <svr  2008-04-16 18:01:13 
Re: Less greedy pattern match
Ed Morton <morton@[EMA  2008-04-16 23:48:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 6:31:33 CDT 2008.