From: Otavio Exel on
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 "<a,b>" with "<b,a>";

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(a)economatica.com.br
From: Wayne on
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 "<a,b>" with "<b,a>";
>
> 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
From: Wayne on
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 "<a,b>" with "<b,a>";
>
> 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 '<a,b>' |awk '
{result=gensub(/<(.*),(.*)>/, "<\\2,\\1>",1); $0=result}1'
<b,a>

-Wayne
From: Otavio Exel on
> 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 <nospam(a)all4me.invalid> 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(a)economatica.com.br
From: Ed Morton on


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 <nospam(a)all4me.invalid> 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 "<a,b>" with "<b,a>";

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

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

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 "<a,b>" | awk -F'[<>,]' '{print "<"$3","$2">"}'
<b,a>

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.