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.
Roberto Zuelli <roberto...@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.
> > 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.
> > 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.
> On Oct 15, 5:51 pm, "Ed Morton" <mortons...@gmail.com> wrote:
>> Roberto Zuelli <roberto...@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.
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
> 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
> > 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
> 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
> The command that you gave me will not work correctly if I've something like this:
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...
>>> 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
>> 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
>> 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?
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.
> >>> 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
> >> 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
> >> 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?
> Certainly it does, and it will also print BUS7, as it is in your newly
> presented data. - Have you tried my script at all?
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).
>>>>> 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
>>>> 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
>>>> 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?
>> Certainly it does, and it will also print BUS7, as it is in your newly
>> presented data. - Have you tried my script at all?
> 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...
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.
> >>>>> 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
> >>>> 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
> >>>> 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?
> >> Certainly it does, and it will also print BUS7, as it is in your newly
> >> presented data. - Have you tried my script at all?
> > 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...
> 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...
> >> 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
> Once again: Please provide appropriate sample data; input and requested
> output data corresponding to your presented input data.
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).
> 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
> On Oct 16, 5:54 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> 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).
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)
> > 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
> 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
If I don't use the ";" I get error while if I use it I get the output
previously specified.
> 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).
> On Oct 16, 6:26 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
> wrote:
>> 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
>> 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
> If I don't use the ";" I get error while if I use it I get the output
> previously specified.
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.
> > 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.
> 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.
> > 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!