bash sum grouping loop -


i have helpfile1 of format:

client1 bla blahblah 2542 kb client1 bla blahblah 4342 mb client1 bla blahblah    7 gb  client2 bla blahblah  455 mb client2 bla blahblah  455 mb 

...

and need weeklysize

client1 sum xy kb client2 sum yx kb 

currently im using:

sumfunction ()     {     inputfile=helpfile1      in `awk -f":" '{print $1}' $inputfile| sort -u | xargs`         awk -v name=$i 'begin {sum=0};     $0~name {     print $0;     if ($5 == "gb") sum = sum + $4*1024*1024;     if ($5 == "mb") sum = sum + $4*1024;     if ($5 == "kb") sum = sum + $4};     end {print name " sum " sum " kb"}' $inputfile     done     }     sumfunction | grep sum | sort -g -r -k 3 > weeklysize 

i need use on pretty long file , awk taking time. there code (bash only), done faster? thank you

you can use following awk script:

awk '/mb$/{$4*=1024};/gb$/{$4*=1024*1024};{a[$1]+=$4}end{for(i in a){printf "%s %s kb\n",i, a[i]}}' a.txt  

looks better in format:

/mb$/    {$4*=1024};        # handle mb /gb$/    {$4*=1024*1024};   # handle gb  # count kb amount client {a[$1]+=$4}  end{     for(i in a){         printf "%s %s kb\n",i, a[i]     } }  

output

client1 11788782 kb client2 931840 kb 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -