| | |
turn mb to gb!
Thread Solved |
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
hi,
i am totally stuck i need a script (that works in ksh under solaris 10) that takes the following output
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 :
any help??
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)
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
Shell Scripting Syntax (Toggle Plain Text)
metastat -c | grep "s" | grep $DISK | awk '{print $1,$3,$4}'\ > ${UFSDISKSLICE}_${DISK}_${Date}
any help??
If my post helped add to my rep!
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)
#!/usr/bin/perl push @lines, "d15 509MB c1t0d0s5"; push @lines, "d13 7.0GB c1t0d0s3"; push @lines, "d11 1.5GB c1t0d0s1"; push @lines, "d10 10GB c1t0d0s0"; push @lines, "d25 509MB c1t1d0s5"; push @lines, "d23 7.0GB c1t1d0s3"; push @lines, "d21 1.5GB c1t1d0s1"; push @lines, "d20 10GB c1t1d0s0"; foreach $line (@lines) { ($dcode, $unit, $drive) = split(/\s/, $line); if (substr($unit, (length($unit) -2), 2) eq "GB") { $unit = substr($unit, 0, length($unit) -2); $unit = ($unit * 1024); # // Convert To MB } else { $unit = substr($unit, 0, (length($unit) -2)); } print "$dcode\t$unit\t$drive\n"; }
Assuming data format:
For example purposes coming from datafile to awk and displaying to standard output. Modify to need.
No need of calling grep to pipe only lines containing an s at column $3
•
•
•
•
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
Shell Scripting Syntax (Toggle Plain Text)
awk '/s/ { if ( index($2, "GB") ) { $2 = sprintf("%10.2f", ($2*1024)) } else { $2 = sprintf("%10.2f", $2) } print $0 }' datafile
Shell Scripting Syntax (Toggle Plain Text)
/* output example d15 509.00 c1t0d0s5 d13 7168.00 c1t0d0s3 d11 1536.00 c1t0d0s1 d10 10240.00 c1t0d0s0 d25 509.00 c1t1d0s5 d23 7168.00 c1t1d0s3 d21 1536.00 c1t1d0s1 d20 10240.00 c1t1d0s0 */
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
Hi,
I get the following output:
Im using solaris 10 and the script is running with KSH could this be causing it?
I get the following output:
Shell Scripting Syntax (Toggle Plain Text)
d15 0.00 c1t0d0s5 d13 0.00 c1t0d0s3 d11 0.00 c1t0d0s1 d10 0.00 c1t0d0s0 d25 0.00 c1t1d0s5 d23 0.00 c1t1d0s3 d21 0.00 c1t1d0s1 d20 0.00 c1t1d0s0
If my post helped add to my rep!
•
•
•
•
Hi,
Im using solaris 10 and the script is running with KSH could this be causing it?
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.
•
•
Join Date: Feb 2006
Posts: 399
Reputation:
Solved Threads: 14
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
If I take out the sprintf line and just use print $2 at end of the script I get the following:
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?
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)
#!/bin/ksh awk '/s/ { if ( index($2, "GB") ) { $2 = sprintf("%10.2f", ($2*1024)) } else { $2 = sprintf("%10.2f", $2) } print $0 }' datafile
Shell Scripting Syntax (Toggle Plain Text)
509MB 7.0GB 1.5GB 10GB 509MB 7.0GB 1.5GB 10GB
Last edited by chris5126; Mar 20th, 2009 at 11:41 am.
If my post helped add to my rep!
>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
...and watch the result.
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)
echo "1.2GB" | awk '{print sprintf("$8.2f", $1)}'
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Similar Threads
- Turn Off Indexing to Speed Up XP (Windows tips 'n' tweaks)
- New Monitor takes 2 boots to turn on (Monitors, Displays and Video Cards)
- Turn Off System Restore to Save Space (Windows tips 'n' tweaks)
- Urgent: New G5 won't turn on! (Apple Hardware)
- Turn Off File Names in Thumbnail View (Windows tips 'n' tweaks)
- Turn on ClearType Font-Rendering Technology (Windows tips 'n' tweaks)
- Turn Off Display and Select an Animated Character in Search Companion in Windows XP (Windows tips 'n' tweaks)
- Turn Off Autoplay for Program CDs (Windows tips 'n' tweaks)
- how do i turn email notifications off on this forum? (Windows NT / 2000 / XP)
Other Threads in the Shell Scripting Forum
- Previous Thread: Help! extract picture from a multi-page pdf/ps file
- Next Thread: Script to create directory structure
| Thread Tools | Search this Thread |






