 |
|
 |
|
|
 |
adding up field columns in a file |
 |
 |
|
|
01-23-06 07:55 AM
Hello,
I have a file with some columns of numeric data e.g.
5 5
5 6
5 7
I need to total them, currently im using the following method
for i in cat <filename> | awk print column2 (for example)
do
total=3D$i
total=3D=E8xpr $total + $i`
done
However this method is really slow on large files, could someone
recommend a quicker way?
Thanks
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-23-06 12:52 PM
On 2006-01-23, cconnell_1@lycos.com wrote:
> Hello,
> I have a file with some columns of numeric data e.g.
>
> 5 5
> 5 6
> 5 7
>
> I need to total them, currently im using the following method
>
>
> for i in cat <filename> | awk print column2 (for example)
> do
> total=$i
> total=èxpr $total + $i`
> done
>
> However this method is really slow on large files, could someone
> recommend a quicker way?
To add the second column:
awk -v col=2 '{ total += $col } END { print total }' FILENAME
To add the first column, change col=2 to col=1
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-23-06 12:52 PM
Chris F.A. Johnson wrote:
> On 2006-01-23, cconnell_1@lycos.com wrote:
>
> To add the second column:
>
> awk -v col=3D2 '{ total +=3D $col } END { print total }' FILENAM
E
>
> To add the first column, change col=3D2 to col=3D1
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence
Thanks, I think there is a typo somewhere as I get byte@osn01:/tmp-->
awk -v col=3D2 '{ total +=3D $col } END { print total }' test
awk: syntax error near line 1
awk: bailing out near line 1
Any ideas?
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-23-06 12:52 PM
On 2006-01-23, cconnell_1@lycos.com wrote:
>
> Chris F.A. Johnson wrote:
>
> Thanks, I think there is a typo somewhere as I get byte@osn01:/tmp-->
> awk -v col=2 '{ total += $col } END { print total }' test
> awk: syntax error near line 1
> awk: bailing out near line 1
>
> Any ideas?
Are you using Solaris? If so, use nawk or xpg4/awk (I forget the
exact location).
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-23-06 12:52 PM
>>>>> "cconnell" == cconnell 1 <cconnell_1@lycos.com> writes:
cconnell> for i in cat <filename> | awk print column2 (for example)
Why the unnecessary cat?
Why not just:
cat filename | cat | cat | cat | tr | cat | awk ...
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment PERL training
!
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-23-06 11:13 PM
On 2006-01-23, Randal L. Schwartz wrote:
>
>cconnell> for i in cat <filename> | awk print column2 (for example)
>
> Why the unnecessary cat?
>
> Why not just:
>
> cat filename | cat | cat | cat | tr | cat | awk ...
usage: tr [-csu] string1 string2
tr [-cu] -d string1
tr [-cu] -s string1
tr [-cu] -ds string1 string2
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-24-06 07:49 AM
While on the subject, if one wants to print selective columns in a file
using awk, is it possible to do it without cat to make it faster?
i.e I always pipe the output from a file into awk from cat, can you use
awk on its own?
Thanks
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-24-06 12:51 PM
cconnell_1@lycos.com wrote:
> While on the subject, if one wants to print selective columns in a file
> using awk, is it possible to do it without cat to make it faster?
> i.e I always pipe the output from a file into awk from cat, can you use
> awk on its own?
Yes. In general, you can almost always eliminate redundant "cat"s
by changing this form:
cat foo | bar
into this form:
bar < foo
With awk, there is an even shorter way, because awk can take an input
filename as a command line argument directly (just like cat does), so
you can change this
cat /etc/passwd | awk -F: '{print $1}'
into this:
awk -F: '{print $1}' /etc/passwd
You could of course use redirection so that the shell opens the file
and makes it awk's standard input:
awk -F: '{print $1}' < /etc/passwd
but that more typing.
- Logan
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: adding up field columns in a file |
 |
 |
|
|
01-24-06 12:51 PM
On 2006-01-24, cconnell_1@lycos.com wrote:
> While on the subject, if one wants to print selective columns in a file
> using awk, is it possible to do it without cat to make it faster?
> i.e I always pipe the output from a file into awk from cat, can you use
> awk on its own?
Did you look at the solutions you were given? Don't they answer
your question?
--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 07:39 PM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|