1828038 Members
1979 Online
109973 Solutions
New Discussion

Re: perl script reformat

 
SOLVED
Go to solution
Matthew_50
Valued Contributor

perl script reformat

## start of input file
DISKPART>
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 G Data File NTFS Partition 1000 GB Healthy
Volume 1 H Quorum NTFS Partition 1020 MB Healthy
Volume 2 Partition 2000 GB Healthy
Volume 3 C NTFS Partition 137 GB Healthy System

DISKPART>

## end of input file

using perl,


if "Size" column equals to 2000GB, then output
"select volume 2"
"assign letter=G"
if "size" column equals to 1000GB, then output
"select volume 0"
"assign letter=H"
if "size" column equals to 1020MB, then output
"select volume 1"
"assign letter=Q"

Typically, when running the sciprt, should be able to read from input file,
reformat, then output like following; the input file content will dynamicly changes

select volume 2
assign letter=G
select volume 0
assign letter=H
select volume 1
assign letter=G
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: perl script reformat

Hi Matthew:

You could do something like this:

# cat ./reformat
#!/usr/bin/perl
use strict;
use warnings;
my ( $size, $units );;
while (<>) {
next if $. < 3; #...skip two header lines...

( $size, $units ) = m{\s(\d+)\s+(.B)\s};

if ( $size == 2000 and $units eq 'GB' ) {
print "select volume 2\nassign letter=G\n"
}
elsif ( $size == 1000 and $units eq 'GB' ) {
print "select volume 0\nassign letter=H\n"
}
elsif ( $size == 1020 and $units eq 'MB' ) {
print "select volume 1\nassign letter=Q\n"
}
}
1;

...run as :

# ./reformat file

Regards!

...JRF...
Matthew_50
Valued Contributor

Re: perl script reformat

Hello James,

in the output "select volume 2", string "2" will refer to original input file field 2.

for 2000GB volume, it will dedicate map to G
for 1000GB volume, it will dedicate map to H
for 1020MB volume, it will dedicate map to Q

but input file, the first and the second field will change dynamically.

Thank you.
James R. Ferguson
Acclaimed Contributor
Solution

Re: perl script reformat

Hi (again):

> in the output "select volume 2", string "2" will refer to original input file field 2.

You didn't make that very clear, originally. Good specifications make for good code.

Here's the revised version which tolerates fuzzier input. Note that a valid line begins with "Volume" and can be found to contain whitespace delimited "size" and "units" fields.

# cat ./reformat
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
my ( $volno, $size, $units ) = m{^Volume\s(\d+)\s+.+?(\d+)\s*(.B)\s};
next unless defined $size;
if ( $size == 2000 && $units eq 'GB' ) {
print "select volume $volno\nassign letter=G\n"
}
elsif ( $size == 1000 && $units eq 'GB' ) {
print "select volume $volno\nassign letter=H\n"
}
elsif ( $size == 1020 && $units eq 'MB' ) {
print "select volume $volno\nassign letter=Q\n"
}
}
1;

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: perl script reformat

If the data to parse (that table) is a list with fixed-width columns (you didn't tick "retain format", so we cannot see), the best way to pare the lines is using unpack.

Volume ### Ltr Label Fs Type Size Status Info

my %size = (
kb => 1024,
mb => 1024 * 1024,
gb => 1024 * 1024 * 1024,
);

while (<>) {
my ($vol, $xxx, $ltr, $lbl, $fs, $type, $size, $status, $info) = unpack "A11 A3 A11 A5 A10 A7 A9 A*", $_;

$size =~ s/^\s*(\d+)\s*([kKmMgG][bB])/$1*$size{lc$2}/e;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Matthew_50
Valued Contributor

Re: perl script reformat

Thanks, James !!

After some modify, it matches my requirement.