awk programming question

Warren Block wblock at wonkity.com
Thu Jan 23 22:42:24 UTC 2014


On Thu, 23 Jan 2014, dteske at FreeBSD.org wrote:

>
>
>> -----Original Message-----
>> From: Warren Block [mailto:wblock at wonkity.com]
>> Sent: Thursday, January 23, 2014 12:57 PM
>> To: 'Devin Teske'
>> Cc: 'RW'; freebsd-questions at freebsd.org
>> Subject: RE: awk programming question
>>
>> On Thu, 23 Jan 2014, dteske at FreeBSD.org wrote:
>>
>>>> From: RW [mailto:rwmaillists at googlemail.com]
>>>> Note that awk supports +, but not newfangled things like *.
>>>
>>> With respect to regex, what awk really needs is the quantifier syntax...
>>>
>>> * = {0,} = zero or more
>>> + = {1,} = one or more
>>> {x,y} = any quantity from x inclusively up to y {x,} = any quantity
>>> from x or more
>>
>> I think RW meant to type that awk did not have the newfangled "?" for non-
>> greedy matches.
>
> But that one is supported. Tested on 9.2-R and 10.0-R...
>
> echo abbb | awk '{sub(/a?bbb/, ""); print}' # produces NULL output
> echo bbb | awk '{sub(/a?bbb/, ""); print}' # similarly produces NULL output
>
> Seems to be supported. But I'd really like to see {x,y} (the ? is equivalent
> to {0,1}).

No, the non-greedy modifier to a standard quantifier:

echo "abczabczabcz" | perl -ne '/(a.*z)/; print "$1\n"'
abczabczabcz

echo "abczabczabcz" | perl -ne '/(a.*?z)/; print "$1\n"'
abcz


More information about the freebsd-questions mailing list