LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   awk puzzle (https://www.linuxquestions.org/questions/linux-general-1/awk-puzzle-831022/)

dazdaz 09-08-2010 12:14 PM

awk puzzle
 
Hi, I am struggling to write an awk script with the correct syntax and wondered if anyone knows this really well.

I have 2 columns of data like this below which I would like to addition.

229K 128M
2.41G 4G
126M 2G
2.86G 0
2.69G 4G
175M 2G
2.86G none

1. Convert none to 0
2. If mb(M) then do nothing. If kb(K), or gb(G) then convert to mb
3. Addition the 1st and 2nd column and print output in gb

Code:

cat file | sed -e 's/none/0/g' | nawk -F" " '{if($1 | $2 ~ /G/) {printf "%.2f %.2f\n", $1*1024, $2} elif {if($1 | $2 ~ /K/) {printf "%.2f %.2f\n", $1/1024/1024, $2}}' else 'print $1, $2' | nawk -F" " '{sum += $1, sum2 =+ $2}END{ printf("Used : %12.fGB Quota : %12.fGB\n",sum/1024, sum2/1024)}'

goldenbarb 09-08-2010 04:23 PM

Code:

{
if ( $1 ~ /none/ ) {  sub(/none/, "0", $1) ; arr1=$1}
if ( $2 ~ /none/ ) { sub(/none/, "0", $2); arr2=$2 }
if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2 }
if ( $1 ~ /M/ ) { sub(/M/, "", $1) ; arr1=$1 }
if ( $2 ~ /M/ ) { sub(/M/, "", $2) ; arr2=$2 }
if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2}
if ( $1 ~ /G/ ) { sub(/G/, "", $1) ; arr1=$1*1024 }
if ( $1 ~ /K/ ) { sub(/K/, "", $1) ; arr1=$1/1024 }
if ( $2 ~ /G/ ) { sub(/G/, "", $2) ; arr2=$2*1024 }
if ( $2 ~ /K/ ) { sub(/K/, "", $2) ; arr2=$2/1024 }
arr3=arr1+arr2
printf "%.2f %.2f %.2f\n", arr1  , arr2  , arr3
}

Code:

0.22 128.00 128.22
2467.84 4096.00 6563.84
126.00 2048.00 2174.00
2928.64 0.00 2928.64
2754.56 4096.00 6850.56
175.00 2048.00 2223.00
2928.64 0.00 2928.64

It's not best way but should work.
Besides it should work only for upper-case M and K.
It can be fixed by tolower function.

kurumi 09-08-2010 06:59 PM

Code:

sed  's/^/((/;s/$/))\/1024000/;s/ \+/)+(/g;s/none/0/;s/K/*1024/g;s/G/*1024000/g;s/M//g' file |bc

dazdaz 09-09-2010 04:20 AM

Hi goldenbarb, nice post thanks for the awk, that's something that I can re-use later on.

However I have to addition the total of column 1 and the total of column 2 instead of adding up column1 and column2, and outputting this into column 3.

I tried using the following, but the numbers didnt add up.

sum1+=$arr1
sum2+=$arr2

druuna 09-09-2010 04:50 AM

Hi,

Building on goldenbarb's code from post #2:
Code:

#!/bin/bash

awk '{ if ( $1 ~ /none/ ) {  sub(/none/, "0", $1) ; arr1=$1}
if ( $2 ~ /none/ ) { sub(/none/, "0", $2); arr2=$2 }
if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2 }
if ( $1 ~ /M/ ) { sub(/M/, "", $1) ; arr1=$1 }
if ( $2 ~ /M/ ) { sub(/M/, "", $2) ; arr2=$2 }
if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2}
if ( $1 ~ /G/ ) { sub(/G/, "", $1) ; arr1=$1*1024 }
if ( $1 ~ /K/ ) { sub(/K/, "", $1) ; arr1=$1/1024 }
if ( $2 ~ /G/ ) { sub(/G/, "", $2) ; arr2=$2*1024 }
if ( $2 ~ /K/ ) { sub(/K/, "", $2) ; arr2=$2/1024 }
arr3=arr1+arr2
col1=col1+arr1
col2=col2+arr2
col3=col3+arr3
printf "%10.2f%10.2f%10.2f\n", arr1, arr2, arr3
}
END { print "--------------------------------" ;
printf "%9.3fG%9.3fG%9.3fG\n", col1/1024, col2/1024, col3/1024 }
' infile

I'm not sure if you want to add both the rows and the columns, the above does both. The sum of the columns are shown in G.
The bold parts are the additions and/or changes.

Hope this helps.

goldenbarb 09-09-2010 04:58 AM

Code:

{
if ( $1 ~ /none/ ) {  sub(/none/, "0", $1) ; arr1=$1}
if ( $2 ~ /none/ ) { sub(/none/, "0", $2); arr2=$2 }

if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2 }

if ( $1 ~ /M/ ) { sub(/M/, "", $1) ; arr1=$1 }
if ( $2 ~ /M/ ) { sub(/M/, "", $2) ; arr2=$2 }

if ( $1 == 0 ) { arr1=$1}
if ( $2 == 0 ) { arr2=$2}

if ( $1 ~ /G/ ) { sub(/G/, "", $1) ; arr1=$1*1024 }
if ( $1 ~ /K/ ) { sub(/K/, "", $1) ; arr1=$1/1024 }

if ( $2 ~ /G/ ) { sub(/G/, "", $2) ; arr2=$2*1024 }
if ( $2 ~ /K/ ) { sub(/K/, "", $2) ; arr2=$2/1024 }

sum1=sum1+arr1
sum2=sum2+arr2
printf "%.2f %.2f\n", arr1  , arr2
}
END{print "------"; printf "%.2f %.2f\n", sum1  , sum2}

Code:

0.22 128.00
2467.84 4096.00
126.00 2048.00
2928.64 0.00
2754.56 4096.00
175.00 2048.00
2928.64 0.00
------
11380.90 12416.00

Yet another way :)


All times are GMT -5. The time now is 08:11 AM.