LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 09-08-2010, 12:14 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Rep: Reputation: 17
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)}'
 
Old 09-08-2010, 04:23 PM   #2
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
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.
 
Old 09-08-2010, 06:59 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
sed  's/^/((/;s/$/))\/1024000/;s/ \+/)+(/g;s/none/0/;s/K/*1024/g;s/G/*1024000/g;s/M//g' file |bc
 
Old 09-09-2010, 04:20 AM   #4
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Original Poster
Rep: Reputation: 17
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
 
Old 09-09-2010, 04:50 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-09-2010, 04:58 AM   #6
goldenbarb
Member
 
Registered: Aug 2010
Distribution: Fedora, Centos, Debian
Posts: 49

Rep: Reputation: 7
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
A puzzle Gins Linux - General 10 02-24-2007 07:23 AM
A Laptop Puzzle emcoder123 Slackware 10 09-02-2005 08:44 AM
GTK puzzle Chuck23 Fedora 9 08-24-2004 03:04 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration