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

Sum a numbers of a row

123 views
Skip to first unread message

Roberto Zuelli

unread,
Oct 15, 2012, 11:30:31 AM10/15/12
to
Hello,
I've a txt.file like this:
A 1 2 3 0 0 6
B 0 0 0 0 0 0
J 0 0 0 1 0 9
D 0 0 0 0 0 0
O 0 99 0 1 0 9
Q 13 0 0 1 0 9
Z 0 0 5 8 0 8

The dimension of the table (rows and columns) aren't known, the goal
is make the sum of the elements for every row and print out the name
of the firtst row (i.e A,B,J...) with sum =0 (In this case B).
In the table there are more than different rows with all elemets =0
but my objective is to identify the first one.
I use awk in another language (GAMS) so the commands used are into
$onecho and $offecho.

Any idea?

Thanks

Roberto

Ed Morton

unread,
Oct 15, 2012, 12:51:22 PM10/15/12
to
Roberto Zuelli <rober...@gmail.com> wrote:

> Hello,
> I've a txt.file like this:
> A 1 2 3 0 0 6
> B 0 0 0 0 0 0
> J 0 0 0 1 0 9
> D 0 0 0 0 0 0
> O 0 99 0 1 0 9
> Q 13 0 0 1 0 9
> Z 0 0 5 8 0 8
>
> The dimension of the table (rows and columns) aren't known, the goal
> is make the sum of the elements for every row and print out the name
> of the firtst row (i.e A,B,J...) with sum =0 (In this case B).
> In the table there are more than different rows with all elemets =0
> but my objective is to identify the first one.

awk '{sum=0; for (i=2;i<=NF;i++) sum+=$i} !sum{print $1; exit}' file

> I use awk in another language (GAMS) so the commands used are into
> $onecho and $offecho.

No idea what that means.

Ed.

> Any idea?
>
> Thanks
>
> Roberto
>


Posted using www.webuse.net

Roberto Zuelli

unread,
Oct 15, 2012, 12:57:05 PM10/15/12
to
On Oct 15, 5:51 pm, "Ed Morton" <mortons...@gmail.com> wrote:
> Posted usingwww.webuse.net- Hide quoted text -
>
> - Show quoted text -

$onecho and $offecho mean that I use awk as support language (I call
awk into GAMS to read files).

Roberto Zuelli

unread,
Oct 15, 2012, 1:41:09 PM10/15/12
to
On Oct 15, 5:51 pm, "Ed Morton" <mortons...@gmail.com> wrote:
> Posted usingwww.webuse.net- Hide quoted text -
>
> - Show quoted text -

Considered that I have not too much confidence with awk, could you
explain what means: sum+=$i} !sum{print $1; exit}

Thanks a lot

R.

Luuk

unread,
Oct 15, 2012, 2:18:32 PM10/15/12
to
sum+=$i
is short for
sum=sum+$i

!sum{...}
when sum is 'false', the body is executed...


Luuk

unread,
Oct 15, 2012, 2:23:21 PM10/15/12
to
On 15-10-2012 17:30, Roberto Zuelli wrote:
> I use awk in another language (GAMS)

http://www.gams.com ?

Roberto Zuelli

unread,
Oct 16, 2012, 8:53:50 AM10/16/12
to
Yes Luuk, I use that GAMS...

Thanks everybody for everything.

Roberto

Roberto Zuelli

unread,
Oct 16, 2012, 10:07:33 AM10/16/12
to
Sorry, but I have another problem...
I have a file like this:
Links From To r
AA1 BUS1 BUS2 1
AA2 BUS3 BUS4 2
AA3 BUS2 BUS4 8
AA4 BUS1 BUS3 2
AA5 BUS3 BUS2 1

I'd like to print all the buses that appear in the 2 columns (from and
to) but just one time per each bus:
BUS1
BUS2
BUS3
BUS4
BUS5

Thanks

Roberto

Janis Papanagnou

unread,
Oct 16, 2012, 11:16:51 AM10/16/12
to
I don't see where you've got that BUS5 from.

Here's an awk command to play with...

awk 'NR>1{a[$2];a[$3]}END{for(b in a)print b}'


Janis

>
> Thanks
>
> Roberto
>

Roberto Zuelli

unread,
Oct 16, 2012, 12:10:15 PM10/16/12
to
On Oct 16, 4:16 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> The command that you gave me will not work correctly if I've something like this:
Links From To r
AA1 BUS1 BUS2 1
AA2 BUS3 BUS4 2
AA3 BUS2 BUS4 8
AA4 BUS1 BUS3 2
AA5 BUS5 BUS6 1
AA6 BUS7 BUS5 2
AA7 BUS2 BUS6 1

I mean, it will not print, for example, BUS6, isn't it? (If it is so,
how could I print also the buses that compare in just one column?)
Sorry for the basical question but I'm new with this language...

Janis Papanagnou

unread,
Oct 16, 2012, 12:20:11 PM10/16/12
to
Certainly it does, and it will also print BUS7, as it is in your newly
presented data. - Have you tried my script at all?

> (If it is so,
> how could I print also the buses that compare in just one column?)

Sorry, I don't understand what you are trying to say here. If you have
problems describing it with words, please provide sample data to make
your intention clear. And if I say sample data, I mean sample data that
will be appropriate to show what you want.

> Sorry for the basical question but I'm new with this language...

You're welcome also with basic questions; if you provide sufficient and
appropriate information so that we are able to help you.

Janis

>

Roberto Zuelli

unread,
Oct 16, 2012, 12:46:00 PM10/16/12
to
On Oct 16, 5:20 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
Yes I've tried the command you suggested me but it works in a
particular way: it prints more than 1 time the same bus...


> Sorry, I don't understand what you are trying to say here. If you have
> problems describing it with words, please provide sample data to make
> your intention clear. And if I say sample data, I mean sample data that
> will be appropriate to show what you want.

I've a list of buses in two different columns, my goal is to print 1
time all the possible buses present in the 2 columns
NB certain buses compare just in the first column, certain other in
the second column and certain other in both the columns).

R.

Janis Papanagnou

unread,
Oct 16, 2012, 12:54:35 PM10/16/12
to
No, it does not do that. You must have made some mistake transcribing
the script (or have done some other mistake).

Show exactly the program that you used, show exactly how you called it,
and show exactly the results.

Here for comparison and for your convenience my call...

Input:
awk 'NR>1{a[$2];a[$3]}END{for(b in a)print b}' <<EOT
Links From To r
AA1 BUS1 BUS2 1
AA2 BUS3 BUS4 2
AA3 BUS2 BUS4 8
AA4 BUS1 BUS3 2
AA5 BUS5 BUS6 1
AA6 BUS7 BUS5 2
AA7 BUS2 BUS6 1
EOT

Output:
BUS5
BUS6
BUS7
BUS1
BUS2
BUS3
BUS4


>
>
>> Sorry, I don't understand what you are trying to say here. If you have
>> problems describing it with words, please provide sample data to make
>> your intention clear. And if I say sample data, I mean sample data that
>> will be appropriate to show what you want.
>
> I've a list of buses in two different columns, my goal is to print 1
> time all the possible buses present in the 2 columns
> NB certain buses compare just in the first column, certain other in
> the second column and certain other in both the columns).

Yes, that's what your first requirement was, and that's what my script
does.

It's still not clear what your second requirement of your previous post
was.

Once again: Please provide appropriate sample data; input and requested
output data corresponding to your presented input data.

Janis

>
> R.
>

Roberto Zuelli

unread,
Oct 16, 2012, 1:10:37 PM10/16/12
to
On Oct 16, 5:54 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
The data are the originals.

> It's still not clear what your second requirement of your previous post
> was.
You gave me the answer in the post before (I print all the possible
buses present in the 2 columns).


> Show exactly the program that you used, show exactly how you called it,
> and show exactly the results.

NR>1 {a[$2];a[$3]} end; for(b in a)print b

I put ";" after end because otherwise I get an error (I use awk as
support language in GAMS).

Results:
BUS1
BUS2
BUS3
BUS1
BUS4
BUS2
BUS3

R.

Janis Papanagnou

unread,
Oct 16, 2012, 1:26:37 PM10/16/12
to
On 16.10.2012 19:10, Roberto Zuelli wrote:
> On Oct 16, 5:54 pm, Janis Papanagnou <janis_papanag...@hotmail.com>

>> awk 'NR>1{a[$2];a[$3]}END{for(b in a)print b}'
[...]
>> Show exactly the program that you used, show exactly how you called it,
>> and show exactly the results.
>
> NR>1 {a[$2];a[$3]} end; for(b in a)print b
>
> I put ";" after end because otherwise I get an error (I use awk as
> support language in GAMS).

I said
NR>1{a[$2];a[$3]}END{for(b in a)print b}
and not
NR>1 {a[$2];a[$3]} end; for(b in a)print b

The latter is not even a valid awk program.

Janis

> [...]


Luuk

unread,
Oct 16, 2012, 1:28:16 PM10/16/12
to
and, finally.....

'end' should be i capitals...
like 'END'


Luuk

unread,
Oct 16, 2012, 1:25:31 PM10/16/12
to
You should not do that....

Can you tell us waht the output is of:
awk -V


it should print something like:
GNU Awk 4.0.0
Copyright (C) 1989, 1991-2011 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.



But possibly you have an older version, or.... (somthing else)

Roberto Zuelli

unread,
Oct 16, 2012, 1:36:46 PM10/16/12
to
On Oct 16, 6:26 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
If I don't use the ";" I get error while if I use it I get the output
previously specified.

R.

Janis Papanagnou

unread,
Oct 16, 2012, 1:37:21 PM10/16/12
to
Luuk, that is not the only problem the OP has with his clueless modification.
He also shouldn't terminate the condition by a semicolon, otherwise he'd get
the default action {print $0} if the condition is true; but OTOH, as in his
malformed modification, any undefined variable (like 'end') will be be false
anyway). And he has written the for-loop, which is valid only as action in
an action-part within {...}, stand-alone as condition. There's so many wrong
with that posters code (and posting habits).

Janis

>
>

Janis Papanagnou

unread,
Oct 16, 2012, 1:42:27 PM10/16/12
to
I am going to lose my patience, Roberto. How can we help you, if
you don't give accurate information?! - What "error" do you get?
From what awk version? - Or is it that "GAMS" that has issues?
(But then you should post in a GAMS newsgroup.) I fear that what
you are using might not be a real awk, since it seems to accept
your code which is obviously malformed syntax.

Janis

>
> R.
>

Roberto Zuelli

unread,
Oct 16, 2012, 1:48:04 PM10/16/12
to
On Oct 16, 6:37 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> On 16.10.2012 19:28, Luuk wrote:
>
>
>
>
>
> > On 16-10-2012 19:10, Roberto Zuelli wrote:
> >> On Oct 16, 5:54 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> >> wrote:
> >>> On 16.10.2012 18:46, Roberto Zuelli wrote:
>
> >> NR>1 {a[$2];a[$3]} end; for(b in a)print b
>
> >> I put ";" after end because otherwise I get an error (I use awk as
> >> support language in GAMS).
>
> > and, finally.....
>
> > 'end' should be i capitals...
> > like 'END'
>
> Luuk, that is not the only problem the OP has with his clueless modification.
> He also shouldn't terminate the condition by a semicolon, otherwise he'd get
> the default action {print $0} if the condition is true; but OTOH, as in his
> malformed modification, any undefined variable (like 'end') will be be false
> anyway). And he has written the for-loop, which is valid only as action in
> an action-part within {...}, stand-alone as condition. There's so many wrong
> with that posters code (and posting habits).
>
> Janis
>
>
>
> - Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

I'm using awk as support language and that means that it's recalled by
the principal program (GAMS, for which my company BOUGHT an official
licence!) and for this I do NOT need about all that stuff about which
Luuk speaks!

Janis:
I understand that the semicolon is wrong but if I do not use it I get
the error.

Janis Papanagnou

unread,
Oct 16, 2012, 1:50:16 PM10/16/12
to
On 16.10.2012 19:48, Roberto Zuelli wrote:
>
> Janis:
> I understand that the semicolon is wrong but if I do not use it I get
> the error.

*WHAT* error, please!

>

Roberto Zuelli

unread,
Oct 16, 2012, 1:51:05 PM10/16/12
to
On Oct 16, 6:50 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> - Hide quoted text -
>
> - Show quoted text -

parse error

Janis Papanagnou

unread,
Oct 16, 2012, 1:57:56 PM10/16/12
to
*sigh* - I give up.

Having asked so many times to provide accurate information from which
we can deduce anything, and always just got trash... - good luck with
all you attempt to do in life.

Roberto Zuelli

unread,
Oct 16, 2012, 2:01:28 PM10/16/12
to
On Oct 16, 6:57 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> On 16.10.2012 19:51, Roberto Zuelli wrote:
>
>
>
>
>
> > On Oct 16, 6:50 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> > wrote:
> >> On 16.10.2012 19:48, Roberto Zuelli wrote:
>
> >>> Janis:
> >>> I understand that the semicolon is wrong but if I do not use it I get
> >>> the error.
>
> >> *WHAT* error, please!
>
> >> - Hide quoted text -
>
> >> - Show quoted text -
>
> > parse error
>
> *sigh* - I give up.
>
> Having asked so many times to provide accurate information from which
> we can deduce anything, and always just got trash... - good luck with
> all you attempt to do in life.- Hide quoted text -
>
> - Show quoted text -

Sorry if you've lost your time but all what I wrote was what I had,
nothing more nothing less!

Luuk

unread,
Oct 16, 2012, 2:06:01 PM10/16/12
to
are you REALLY only getting 'parse error' ?

Maybe you better ask on the googlegroups for support on this piece of
GAMS here:
https://groups.google.com/forum/?fromgroups#!forum/gamsworld


Ed Morton

unread,
Oct 16, 2012, 2:32:34 PM10/16/12
to
Janis gave you a perfectly valid script that DOES satisfy your requirements. You
took it and changed the case of END to end and possibly stripped off some curly
braces. In either case, your changing the script caused you to get some
unspecified error message. You then added a semi-colon which presumably got rid
of that error message but left you with non-functional code. The solution is
simple - stop screwing with the code and just use what Janis gave you. If, at
that point you still have a problem then copy the code you are executing along
with the error message and paste it here so we can help you.

Ed.


Posted using www.webuse.net

Luuk

unread,
Oct 16, 2012, 3:37:33 PM10/16/12
to
and, to prove that you need to learn how to give proper information to
get someone else to answer your questions......

i installed GAMS (demo-version)

i created next script (test.gms)
$onecho > c:\temp\x.scr
NR>1{a[$2];a[$3]}
END{for(b in a)print b}
$offecho

$call awk -f c:\temp\x.scr c:\temp\bus.txt

and i created a textfile (c:\temp\bus.txt)
Links From To r
AA1 BUS1 BUS2 1
AA2 BUS3 BUS4 2
AA3 BUS2 BUS4 8
AA4 BUS1 BUS3 2
AA5 BUS3 BUS2 1

the output in GAMS is:
--- Job test.gms Start 10/16/12 21:34:01 WEX-WEI 23.9.3 x86_64/MS
Windows
GAMS Rev 239 Copyright (C) 1987-2012 GAMS Development. All rights reserved
Licensee: GAMS Development Corporation, Washington, DC G871201/0000CA-ANY
Free Demo, 202-342-0180, sa...@gams.com, www.gams.com DC0000
--- Starting compilation
--- test.gms(7) 2 Mb
--- call awk -f c:\temp\x.scr c:\temp\bus.txt
BUS1
BUS2
BUS3
BUS4
--- test.gms(8) 2 Mb
--- call pause
Press any key to continue . . .
--- Starting execution - empty program
*** Status: Normal completion
--- Job test.gms Stop 10/16/12 21:34:01 elapsed 0:00:00.022


So, my (!) conclusion,
you need to learn some basic computer skills before starting to play
with inteligent tools ;)



0 new messages