Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

print one blank line in a for cycle

2 views
Skip to first unread message

Luis P. Mendes

unread,
Nov 22, 2009, 3:25:58 PM11/22/09
to
Hi,

I have this text file:
$ cat aa.txt
sd_1k_ | 3950 | 38 | 126 | 158 | 48 | 78 | 0.0557
| -0.0732 | 0.0675 | 0.1325 | -0.0493 | -0.3721
sd_1l_ | 3950 | 38 | 79 | 74 | 26 | 38 | 0.1051
| -0.0355 | 0.1372 | 0.1112 | -0.0041 | -0.0369
sd_1m__1trim | 3950 | 38 | 18 | 15 | 10 | 7 |
0.0114 | -0.0264 | 0.0150 | 0.0378 | -0.0233 | -0.6164
sd_1n__1mes | 3950 | 38 | 5 | 9 | 5 | 2 |
0.0114 | -0.0141 | 0.0139 | 0.0255 | -0.0099 | -0.3882

and this awk script:
$ cat test.awk

BEGIN {
n_conta[""]=0;
tabelas_conta=0;
}
{
n_conta[$1]+=$4+$5+$6+$7;
desempenho[$1]=$13;
tabelas_conta+=1;
}

END {
print "num tabelas: ",tabelas_conta;

for ( t in n_conta )
print t, n_conta[t], t in n_conta;
}

When I issue this command:
$ awk --field-separator "|" -f test.awk aa.txt

I get this output:
num tabelas: 4
0 1 <----------- why?
sd_1k_ 410 1
sd_1m__1trim 50 1
sd_1n__1mes 21 1
sd_1l_ 217 1

Why do I get the line with the arrow?
What is wrong with my script?


Luis

Grant

unread,
Nov 22, 2009, 4:09:29 PM11/22/09
to

Trailing blank line in data?

Try this just after the BEGIN block:

/^$/ { next } # skip blank lines

Grant.
--
http://bugsplatter.id.au

Luis P. Mendes

unread,
Nov 22, 2009, 4:23:53 PM11/22/09
to
On Mon, 23 Nov 2009 08:09:29 +1100, Grant wrote:

> /^$/ { next } # skip blank lines

No, there are no blank lines. The text is as shown by me in the first
message.

Thank you for the idea, anyway.


Luis

pk

unread,
Nov 22, 2009, 4:24:50 PM11/22/09
to
Luis P. Mendes wrote:

> Hi,
>
> I have this text file:
> $ cat aa.txt
> sd_1k_ | 3950 | 38 | 126 | 158 | 48 | 78 | 0.0557
> | -0.0732 | 0.0675 | 0.1325 | -0.0493 | -0.3721
> sd_1l_ | 3950 | 38 | 79 | 74 | 26 | 38 | 0.1051
> | -0.0355 | 0.1372 | 0.1112 | -0.0041 | -0.0369
> sd_1m__1trim | 3950 | 38 | 18 | 15 | 10 | 7 |
> 0.0114 | -0.0264 | 0.0150 | 0.0378 | -0.0233 | -0.6164
> sd_1n__1mes | 3950 | 38 | 5 | 9 | 5 | 2 |
> 0.0114 | -0.0141 | 0.0139 | 0.0255 | -0.0099 | -0.3882
>
> and this awk script:
> $ cat test.awk
>
> BEGIN {
> n_conta[""]=0;

You are creating an extra element in the n_conta array. Remove the above
line and all will be fine.

Ed Morton

unread,
Nov 22, 2009, 4:41:55 PM11/22/09
to

pk already gave you the answer, but also note that this:

for ( t in n_conta )
print t, n_conta[t], t in n_conta;

can be abbreviated to:

for ( t in n_conta )

print t, n_conta[t], 1

so you may want to rethink that....

Ed.

Luis P. Mendes

unread,
Nov 22, 2009, 6:04:05 PM11/22/09
to

It's an associative array and I tried to initialize it somehow... bad
thought.

Thank you,


Luis

Ed Morton

unread,
Nov 22, 2009, 8:02:56 PM11/22/09
to

Not really a bad idea but it's not necessary in this context. FYI to init an
array you do:

delete array
or
split("",array)

The first one is GNU awk specific.

Ed.

Ed.

Kenny McCormack

unread,
Nov 22, 2009, 10:48:37 PM11/22/09
to
In article <hecn00$sq6$1...@news.eternal-september.org>,
Ed Morton <morto...@gmail.com> wrote:
...

> delete array
>or
> split("",array)
>
>The first one is GNU awk specific.

When most people use the word "specific", they mean "found only in".
Like "the Blarney stone is specific to Ireland".

However, in Usenet newsgroups, the term is often used to mean "found
everywhere except maybe some obscure corner". Such is the case here
(with the "delete array" construct in AWK).

Ed Morton

unread,
Nov 23, 2009, 10:15:23 AM11/23/09
to
On Nov 22, 9:48 pm, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <hecn00$sq...@news.eternal-september.org>,

> Ed Morton  <mortons...@gmail.com> wrote:
> ...
>
> >    delete array
> >or
> >    split("",array)
>
> >The first one is GNU awk specific.
>
> When most people use the word "specific", they mean "found only in".
> Like "the Blarney stone is specific to Ireland".
>
> However, in Usenet newsgroups, the term is often used to mean "found
> everywhere except maybe some obscure corner".  Such is the case here
> (with the "delete array" construct in AWK).

Good point - just because something's a GNU awk extension doesn't mean
it's not available in other awks too. In this case it's supported by
gawk without options and /opt/xpg4/bin/awk, but not supported in GNU
awk with --copmpat or --posix, New awk (nawk) or old, broken awk (no
surprise). I don't know about other modern awks.

Ed.

0 new messages