in reply to Re: Parsing a Large file with no reason
in thread Parsing a Large file with no reason
And I don't know if you can 'tie' the same file (as an array) while opening it for reading (both at the same time).
Note that I set the input record separator, $/, to ---- lsattr, (with a space following lsattr), to read a record at a time.
Not seeing more sample data, I made a guess at what might work and it did work with your sample data. But again, it's difficult to tell.
Update: The data structure created above will only work if there is only 1 record for each sought key, ($vg). If there is more than 1 record with the same key, the data structure will only contain the last fields parsed from the file. It will silently give you incorrect results.#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $base = $ARGV[0] or die "Must supply a filename to open. $!"; open my $fh, "<", $base or die "Unable to locate file: $!\n"; my %data; { local $/ = "---- lsattr "; while (<$fh>) { chomp; next unless /^-El\s+(\S+)/; my $vg = $1; next unless /^label\s+(\S+)/m; my $label = $1; next if $label eq "None"; next unless /^lvserial_id\s+(\S+)/m; my $name = $1; next unless /^size\s+(\d+)/m; my $size = $1; @{ $data{ $vg } }{ qw/ label name size / } = ($label, $name, $ +size); #print "VG: $vg : MOUNT: $label : LV_name: $name : SIZE: $size +\n"; } } print Dumper \%data;
That said, I would need to know more about your file to be able to suggest a suitable data structure.
|
|---|