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
Trailing blank line in data?
Try this just after the BEGIN block:
/^$/ { next } # skip blank lines
Grant.
--
http://bugsplatter.id.au
> /^$/ { 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
> 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.
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.
It's an associative array and I tried to initialize it somehow... bad
thought.
Thank you,
Luis
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.
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.