Re: How awk `split' works?
On 8 Mai, 13:13, PRC <panruoc...@gmail.com> wrote:
[Please don't top-post.]
> I see
> But if FS is space, awk will skip empty fields. Why does awk work in
> different ways for cases where FS is space and where FS is regular
> expression?
In your example you didn't use FS. Generally there are some
special cases implemented with the semantics of FS/RS and
spaces or null strings. I suppose to get the best benefits
from a concise awk interface and powerful features. Besides
that your program behaves exactly the same way if you specify
a space as regexp...
n = split(" a b c d ", a, / /);
In your application, since you know the data and delimiters,
just change your loop
for(i=2; i<n; i++)
Janis
>
> Dave B wrote:
> > On Thursday 8 May 2008 12:49, PRC wrote:
>
> > > The results of running the following script
> > > gawk 'BEGIN {
> > > n = split("(a,b,c,d)", a, /[(,)]/);
> > > printf("n=%d\n", n);
> > > for(i=1; i<=n; i++)
> > > printf(" -%s-\n",a[i]);
> > > }'
> > > is
> > > n=6
> > > --
> > > -a-
> > > -b-
> > > -c-
> > > -d-
> > > --
>
> > > instead of
> > > n=4
> > > -a-
> > > -b-
> > > -c-
> > > -d-
> > > which is expected.
>
> > > I have no ideas how these results come out.
>
> > You are telling awk to use either '(', ',' or ')' as field separator for
> > splitting.
>
> > Given your string '(a,b,c,d)' awk sees six fields:
>
> > - one empty field before the '('
> > - a
> > - b
> > - c
> > - d
> > - one empty field after the ')'
>
> > Try this:
>
> > $ echo '(a,b,c,d)' | awk -F '[(,)]' '{print NF}'
> > 6
>
> > --
> > D
|