943,635 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Mar 8th, 2009
0

turn mb to gb!

Expand Post »
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??
Similar Threads
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 18th, 2009
0

Re: turn mb to gb!

can no one help??
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 19th, 2009
0

Re: turn mb to gb!

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:
perl Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 19th, 2009
1

Re: turn mb to gb!

Assuming data format:
Quote ...
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. */
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 20th, 2009
0

Re: turn mb to gb!

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?
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 20th, 2009
0

Re: turn mb to gb!

Click to Expand / Collapse  Quote originally posted by chris5126 ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 20th, 2009
0

Re: turn mb to gb!

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.
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 20th, 2009
0

Re: turn mb to gb!

>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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 20th, 2009
0

Re: turn mb to gb!

hi,

Just tried that from the command line and I still get 0.00! any ideas what could be doing it?
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Mar 23rd, 2009
0

Re: turn mb to gb!

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Help! extract picture from a multi-page pdf/ps file
Next Thread in Shell Scripting Forum Timeline: Script to create directory structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC