Operating System - HP-UX
1753781 Members
7328 Online
108799 Solutions
New Discussion юеВ

Re: from horizon text format into vertical text format

 
SOLVED
Go to solution
Matthew_50
Valued Contributor

Re: from horizon text format into vertical text format

Let me re-summarize,

input as attach sample file.

1. extract line 17 to line 57
2. reformat & convert into following output

slot1/0:0
slot1/1:0
.
.
slot1/total:0
slot4/0:0
.
.
slot4/25:4300
.
slot4/27:4300
.
slot4/total:17000
.
.
slot9/16:144000000
.
.
slot9/19:67000000
slot9/total:423000000
Hein van den Heuvel
Honored Contributor

Re: from horizon text format into vertical text format

Ok... so there is no need for filter for line 17 - 57.
That's just a crazy restriction to get yo into trouble later.
What you really want is to recognize the report pattern, ending in 'total' like my example already did.
It simply ignores the noise before ( $. < 17) and after ( $. > 57 )

I failed to see the decimal value requirement, and so it seems did Jim.
The \d+ use looks for charactes 0 thru 9, but here we need to use [0-9.]+ to add the .

One possible solution then becomes:

------------------ reformat.pl ----
while (<>) {
push @tag,split if /^\s*\d+\s+/;
if (/^(sl.*):(.*)/) {
$nam=$1;
$_ = $2;
push @val,split;
#debug print " -- $#tag -- @tag[-1] --", join '*',@tag, "\n";
if (@tag[-1] eq 'Total') {
for $i ( 0 .. $#val) {
$_ = $val[$i];
s/([0-9.]+)k/$1*1000/e;
s/([0-9.]+)m/$1*1000*1000/e;
print "$nam/$tag[$i]:$_\n";
}
@tag = @val = ();
}
}
}



Run as: perl reformat.pl < data.file

Or embed in a shell script with the shebang: #!/usr/bin/env perl ...

Btw... if you want to surpress all lines with zeros, change the print to:

$_ and print "$nam/$tag[$i]:$_\n";

Add back in all 'total' lines with:

($_ or $tag[$i] eq 'Total') and print "$nam/$tag[$i]:$_\n";


Hein.
Matthew_50
Valued Contributor

Re: from horizon text format into vertical text format

hmm, after some try & error.

see attach for the result.
Matthew_50
Valued Contributor

Re: from horizon text format into vertical text format

Thank you JRF, Hein being with me so late,
almost time for me to start work. @@~
James R. Ferguson
Acclaimed Contributor

Re: from horizon text format into vertical text format

Hi Matthew:

I had to abandon you yesterday for family-time. Hein was correct, I failed to see the decimal requirement let alone the last code I posted wasn't quite what we needed.

That said, here's a version that handles integer or decimal values with or without a suffix of "k" or "m". The suffix is evaluated case-insensitively. The output is expressed in integer values, rounded up to the nearest integer. Once again I have used the multiplicative factor of 1024 in lieu of 1000. In all:

# cat ./reformat
#!/usr/bin/perl
use strict;
use warnings;
my ( @first, @second, $slot, $n, $value );
while (<>) {
next if /^===/;
if (m{^\s*\d}) {
@first = split;
next;
}
if (m{^slot}) {
@second = split;
$slot = shift @second;
( $slot = shift @second ) =~ s/://;
}
if ( m{^\s*$} or eof ) {
for ( $n = 0; $n < @first; $n++ ) {
$value = $second[$n];
if ( $value =~ m/k$/i ) {
$value =~ s/((\d+)(\.\d+)?)k/$1*1024/ie;
}
elsif ( $value =~ m/m$/i ) {
$value =~ s/((\d+)(\.\d+)?)m/$1*1024*1024/ie;
}
printf "slot %s/%s:%-8.0f\n", $slot, $first[$n], $value;
}
}
}
1;

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: from horizon text format into vertical text format


This is probably lost but just in case a future reader gets this far...

An other way to deal with these suffixes representing multipliers is a table lookup.
This avoids (nested) ifs.
To deal with a missing multiplier append a special characted (or use the newline if there ;-)

Something along the lines of

%mul=qw(x 1 k 1000 m 1000 g 1000000000);

$_ = $text . q(x); # append an 'x' just in case.
m/([0-9.]+)(.)/; # find numbers and multiplier
$value = $1 * $ mul{lc($2)} # and the real value is...

Here is a one-liner if you want to try this

# perl -le %mul=qw(x 1 k 10 m 100 g 1000); while(<>) { chomp; $_.=q(x); m/([0-9.]+)(.)/; print $1*$
mul{$2};}'

fwiw,
Hein.