Computing.Net > Forums > Unix > working with column

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

working with column

Reply to Message Icon

Name: sarahq
Date: November 19, 2007 at 10:57:57 Pacific
OS: unix
CPU/Ram: 30
Product: 30
Comment:

I have a file with content

1 arnone 100
1 arnone 100
4 temp 20
2 temp 10
... and so on

2 arnone 200
6 temp 30

how do i achieve this result?

thanks in advance




Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: November 19, 2007 at 12:35:40 Pacific
Reply:

I do no think you stated what you want your result to be.


0

Response Number 2
Name: sarahq
Date: November 19, 2007 at 12:58:29 Pacific
Reply:

the result should be

2 arnone 200
6 temp 30

FROM

1 arnone 100
1 arnone 100
4 temp 20
2 temp 10



0

Response Number 3
Name: lankrypt0
Date: November 19, 2007 at 14:06:54 Pacific
Reply:

Try:

#!/usr/bin/ksh
for var in $(cat tfile|awk '{print $2}'|sort -u);do
grep $var tfile > temp.file
ftot=0
stot=0
while read line;do
fnum=$(print $line|awk '{print $1}')
snum=$(print $line|awk '{print $3}')
ftot=$(($ftot+$fnum))
stot=$(($stot+$snum))
done < temp.file
print $ftot $var $stot
done


0

Response Number 4
Name: James Boothe
Date: November 21, 2007 at 16:19:31 Pacific
Reply:

This is a standard control-break approach.  When the key field changes, a summary line is printed and the accumulators reset to zero.

It assumes that the input file is already sorted by the key field.  If not, you could insert a sort command in this script.

Instead of shell script, awk could do the same control-break processing on a sorted file.  Or awk could process an unsorted file, accumulating the values in an array, and that would actually be less code.

#!/bin/bash

holdkey=
ftot=0
stot=0

while read num1 key num2
do
if [ "$key" != "$holdkey" ] ; then
   if [ "$holdkey" != "" ] ; then
      echo $tot1 $holdkey $tot2
   fi
   holdkey=$key
   tot1=0
   tot2=0
fi
((tot1=tot1+num1))
((tot2=tot2+num2))
done < filein

# print totals for final key group
echo $tot1 $holdkey $tot2

2 arnone 200
6 temp 30


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Help with shell script sed bug?



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: working with column

Help with simple scripting www.computing.net/answers/unix/help-with-simple-scripting/6920.html

Need Help With KSH Script www.computing.net/answers/unix/need-help-with-ksh-script/6747.html

communicate with Linux workstation www.computing.net/answers/unix/communicate-with-linux-workstation/503.html