turn mb to gb!

Thread Solved

Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

turn mb to gb!

 
0
  #1
Mar 8th, 2009
hi,

i am totally stuck i need a script (that works in ksh under solaris 10) that takes the following output
Shell Scripting Syntax (Toggle Plain Text)
  1. d15 509MB c1t0d0s5
  2. d13 7.0GB c1t0d0s3
  3. d11 1.5GB c1t0d0s1
  4. d10 10GB c1t0d0s0
  5. d25 509MB c1t1d0s5
  6. d23 7.0GB c1t1d0s3
  7. d21 1.5GB c1t1d0s1
  8. d20 10GB c1t1d0s0
and for the second column turn all values into MB and removes the MB and GB text. Some are already shown in mb and some are in GB. I think using awk would be the easiest but awk is not my strong point. The command i use to get the output in the first place is :
Shell Scripting Syntax (Toggle Plain Text)
  1. metastat -c | grep "s" | grep $DISK | awk '{print $1,$3,$4}'\
  2. > ${UFSDISKSLICE}_${DISK}_${Date}

any help??
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: turn mb to gb!

 
0
  #2
Mar 18th, 2009
can no one help??
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: turn mb to gb!

 
0
  #3
Mar 19th, 2009
One of the biggest problems here, is getting the shell to handle floating point numbers. I'm not using / have access to a solaris machine, so I'm going strictly of bash in slackware.... but the shell doesn't handle floating point numbers. In a Perl script, I could knock this out, or in a C++ program, it would be a breeze... but doing this as a shell script, is an entirely different beast. Parsing the data with awk or sed would be alright, but converting the numbers is an entirely different challenge. In perl, it is pretty easy:
  1. #!/usr/bin/perl
  2.  
  3. push @lines, "d15 509MB c1t0d0s5";
  4. push @lines, "d13 7.0GB c1t0d0s3";
  5. push @lines, "d11 1.5GB c1t0d0s1";
  6. push @lines, "d10 10GB c1t0d0s0";
  7. push @lines, "d25 509MB c1t1d0s5";
  8. push @lines, "d23 7.0GB c1t1d0s3";
  9. push @lines, "d21 1.5GB c1t1d0s1";
  10. push @lines, "d20 10GB c1t1d0s0";
  11.  
  12. foreach $line (@lines) {
  13. ($dcode, $unit, $drive) = split(/\s/, $line);
  14.  
  15. if (substr($unit, (length($unit) -2), 2) eq "GB") {
  16. $unit = substr($unit, 0, length($unit) -2);
  17. $unit = ($unit * 1024); # // Convert To MB
  18. } else {
  19. $unit = substr($unit, 0, (length($unit) -2));
  20. }
  21.  
  22. print "$dcode\t$unit\t$drive\n";
  23. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: turn mb to gb!

 
1
  #4
Mar 19th, 2009
Assuming data format:
d15 509MB c1t0d0s5
d13 7.0GB c1t0d0s3
d11 1.5GB c1t0d0s1
d10 10GB c1t0d0s0
d25 509MB c1t1d0s5
d23 7.0GB c1t1d0s3
d21 1.5GB c1t1d0s1
d20 10GB c1t1d0s0
For example purposes coming from datafile to awk and displaying to standard output. Modify to need.

Shell Scripting Syntax (Toggle Plain Text)
  1. awk '/s/ {
  2. if ( index($2, "GB") ) {
  3. $2 = sprintf("%10.2f", ($2*1024))
  4. }
  5. else {
  6. $2 = sprintf("%10.2f", $2)
  7. }
  8. print $0
  9. }' datafile
No need of calling grep to pipe only lines containing an s at column $3

Shell Scripting Syntax (Toggle Plain Text)
  1. /* output example
  2. d15 509.00 c1t0d0s5
  3. d13 7168.00 c1t0d0s3
  4. d11 1536.00 c1t0d0s1
  5. d10 10240.00 c1t0d0s0
  6. d25 509.00 c1t1d0s5
  7. d23 7168.00 c1t1d0s3
  8. d21 1536.00 c1t1d0s1
  9. d20 10240.00 c1t1d0s0
  10. */
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: turn mb to gb!

 
0
  #5
Mar 20th, 2009
Hi,

I get the following output:

Shell Scripting Syntax (Toggle Plain Text)
  1. d15 0.00 c1t0d0s5
  2. d13 0.00 c1t0d0s3
  3. d11 0.00 c1t0d0s1
  4. d10 0.00 c1t0d0s0
  5. d25 0.00 c1t1d0s5
  6. d23 0.00 c1t1d0s3
  7. d21 0.00 c1t1d0s1
  8. d20 0.00 c1t1d0s0
Im using solaris 10 and the script is running with KSH could this be causing it?
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: turn mb to gb!

 
0
  #6
Mar 20th, 2009
Originally Posted by chris5126 View Post
Hi,
Im using solaris 10 and the script is running with KSH could this be causing it?
You get that result because sprintf is not receiving any value in column $2 that can convert into a decimal.
For debugging purposes substitute every line with a sprintf() call for just a print of $2 to see the result.

You could also post your script and help us to help you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: turn mb to gb!

 
0
  #7
Mar 20th, 2009
Hi,

To test it I was simply placing the data into a file in /tmp/datafile then the scripts looks like the following its also run from temp
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. awk '/s/ {
  3. if ( index($2, "GB") ) {
  4. $2 = sprintf("%10.2f", ($2*1024))
  5. }
  6. else {
  7. $2 = sprintf("%10.2f", $2)
  8. }
  9. print $0
  10. }' datafile
If I take out the sprintf line and just use print $2 at end of the script I get the following:

Shell Scripting Syntax (Toggle Plain Text)
  1. 509MB
  2. 7.0GB
  3. 1.5GB
  4. 10GB
  5. 509MB
  6. 7.0GB
  7. 1.5GB
  8. 10GB
So sprint if getting the values however they still have either MB or GB on the end so therefore the sprintf statement wouldnt work would it?
Last edited by chris5126; Mar 20th, 2009 at 11:41 am.
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: turn mb to gb!

 
0
  #8
Mar 20th, 2009
>So sprint if getting the values however they still have either MB or GB on the end so therefore the sprintf statement wouldnt work would it?

Yes, it would. sprintf() tries to read and convert any string to the format you give it.
To convince you run
Shell Scripting Syntax (Toggle Plain Text)
  1. echo "1.2GB" | awk '{print sprintf("$8.2f", $1)}'
...and watch the result.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 399
Reputation: chris5126 is an unknown quantity at this point 
Solved Threads: 14
chris5126 chris5126 is offline Offline
Posting Whiz

Re: turn mb to gb!

 
0
  #9
Mar 20th, 2009
hi,

Just tried that from the command line and I still get 0.00! any ideas what could be doing it?
If my post helped add to my rep!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: turn mb to gb!

 
0
  #10
Mar 23rd, 2009
Hey Chris,

It looks like just a simple typo in Aia's reply. Just change $8.2f to %8.2f and you should get "1.20"

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC