1833065 Members
2552 Online
110049 Solutions
New Discussion

Shell Script Assistance

 
SOLVED
Go to solution

Shell Script Assistance

Hi All,

Need some Shell expertise help.
I have input file that contain 2 fields(started date & Size).
input file example:
Started Kbyte
29/6/05 9000
29/6/05 1000
29/6/05 4000
30/6/05 2000
30/6/05 7000

How could I total-up the Size(Kbyte) group by Started field.

TQ.
7 REPLIES 7
Kasper Hedensted
Trusted Contributor

Re: Shell Script Assistance

two quick (& dirty?) solutions:

sum=0
grep -v Kbyte |awk '{print $2}'|while read num
do
sum=$((sum+num))
done
echo $sum

or

echo `grep -v Kbyte | awk '{printf $2;while (getline) { printf "+"$2; }}'` | bc


Cheers
Kasper Hedensted
Trusted Contributor
Solution

Re: Shell Script Assistance

Ups, you need to sort the dates first

dates=`grep -v ^Started |awk '{print $1}' |sort -u`

for date in $dates
do
sum=0
grep ^$date | awk '{print $2}'| while read num
do
sum=$((sum+num))
done
echo $date $sum
done
Peter Nikitka
Honored Contributor

Re: Shell Script Assistance

Hi,

try this:

awk 'NR==1 {unit=$2;next}
{s+=$2}
END {printf("%s %s\n,s,unit)}' inputfile

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: Shell Script Assistance

Hi,

to add the date:

awk 'NR==1 {unit=$2;next}
{s+=$2; date=$1}
END {printf("%s %s %s\n,date,s,unit)}' inputfile
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
curt larson_1
Honored Contributor

Re: Shell Script Assistance

cat yourFile |
awk '
NR=1 {print $0;next;}
{a[$1]=+$2;}
END {
for ( i in a ) print i, a[i];
} | sort -n

Re: Shell Script Assistance

Thanks All,

Another enhancement to the script,

How do I produce the output like this :

Started Kbyte Total
29/6/05 9000
29/6/05 1000
29/6/05 4000 14000
30/6/05 2000
30/6/05 7000 9000

TQ
Rodney Hills
Honored Contributor

Re: Shell Script Assistance

Try-

awk 'prev1!=$1{if (NR > 1) printf " %s",s;prev1=$1;s=0};END{printf " %s\n",s};{s=s+$2;printf "\n%s",$
0}'

HTH

Rod Hills

PS- Assign points as your peers have helped you...
There be dragons...