grouping in awk

grouping in awk

am 14.04.2008 21:14:59 von Otavio Exel

Dear Group,

it is a shame that I can't find the answer to such a simple question!

I read in the awk man pages that awk supports grouping;
but how do I use the parts of the mach?

let's say I'd like to replace "" with "";

in sed I'd do:

s/<\(.*\),\(.*\)>/<\2,\1>/

in awk I expected to do

sub( "<(.*),(.*)>", "<\2,\1>" )

....but (guess?) it didn't work!

any clues?

[]s and TIA!

--
Otavio Exel /<\oo/>\ oexel@economatica.com.br

Re: grouping in awk

am 14.04.2008 21:49:24 von wayne

Otavio Exel wrote:
> Dear Group,
>
> it is a shame that I can't find the answer to such a simple question!
>
> I read in the awk man pages that awk supports grouping;
> but how do I use the parts of the mach?
>
> let's say I'd like to replace "" with "";
>
> in sed I'd do:
>
> s/<\(.*\),\(.*\)>/<\2,\1>/
>
> in awk I expected to do
>
> sub( "<(.*),(.*)>", "<\2,\1>" )
>
> ...but (guess?) it didn't work!
>
> any clues?
>
> []s and TIA!
>

That is a feature of Gnu awk (gawk):

$ echo abc |awk '{result=gensub(/a(b)c/, "x\\1y","g"); print result;}'
xby


-Wayne

Re: grouping in awk

am 14.04.2008 22:09:22 von wayne

Otavio Exel wrote:
> Dear Group,
>
> it is a shame that I can't find the answer to such a simple question!
>
> I read in the awk man pages that awk supports grouping;
> but how do I use the parts of the mach?
>
> let's say I'd like to replace "" with "";
>
> in sed I'd do:
>
> s/<\(.*\),\(.*\)>/<\2,\1>/
>
> in awk I expected to do
>
> sub( "<(.*),(.*)>", "<\2,\1>" )
>
> ...but (guess?) it didn't work!
>
> any clues?
>
> []s and TIA!
>

That is a feature of Gnu awk (gawk):

$ echo '' |awk '
{result=gensub(/<(.*),(.*)>/, "<\\2,\\1>",1); $0=result}1'


-Wayne

Re: grouping in awk

am 17.04.2008 16:18:07 von Otavio Exel

> Otavio Exel wrote:
> >
> > I read in the awk man pages that awk supports grouping;
> > but how do I use the parts of the mach?

Wayne wrote:
>
> That is a feature of Gnu awk (gawk):

Wayne,

I'd rather not use "special" feature;
I'll find a way using sed.

but a very interesting question remains:
- why does awk support grouping than?
- IOW: what can you do with the "groups"?

[]s,

--
Otavio Exel /<\oo/>\ oexel@economatica.com.br

Re: grouping in awk

am 17.04.2008 18:00:11 von Ed Morton

On 4/17/2008 9:18 AM, Otavio Exel wrote:
>>Otavio Exel wrote:
>>
>>>I read in the awk man pages that awk supports grouping;
>>>but how do I use the parts of the mach?
>>
>
> Wayne wrote:
>
>>
>>That is a feature of Gnu awk (gawk):
>
>
> Wayne,
>
> I'd rather not use "special" feature;
> I'll find a way using sed.

That may be the best advice or it may be terrible advice, depending on the
nature of the problem you're trying to solve. There may be a reasonable way to
do what you want in awk without using gawk-specific constructs.

In the part that was snipped you said:

>let's say I'd like to replace "" with "";

That's a simple, single line substitution so it's ideal for sed:

$ echo "" | sed 's/<\(.*\),\(.*\)>/<\2,\1>/'


but if you have anything else you want to do with the input text or if you have
records that span multiple lines then you'd probably want to use awk and then
you'd need to do something like:

$ echo "" | awk -F'[<>,]' '{print "<"$3","$2">"}'


or something else depending on what the rest of your input looked like.

> but a very interesting question remains:
> - why does awk support grouping than?
> - IOW: what can you do with the "groups"?

You group parts of an RE to perform operations on them. For example:

awk '/abc(def|ghi)/'

would print records that contain abcdef and/or abcghi and

awk '/abc(def)?ghi/'

would print records that contain abcdefghi and/or abcghi.

Ed.