in reply to new : greping

Yet another variation. This time to read files formatted per OP's sample data.

use warnings; use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("data.xls"); my $worksheet = $workbook->add_worksheet(); $worksheet->write ("A1", 'Device'); $worksheet->write ("B1", '/var'); $worksheet->write ("C1", '/'); $worksheet->write ("D1", 'interleaved'); my $row = 2; my $line = ''; open inFile, '<', 'data.txt' or die "Couldn't open data.txt: $!"; while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; next if ! ($line =~ /logging to /) and ! eof inFile; if ($line =~ /(\d+)%\s+\/var\s+(\d+).*?(\d+)%\s+Interleaved\s+(\w+)/ +i) { $worksheet->write("A$row", "$4"); $worksheet->write("B$row", "$1"); $worksheet->write("C$row", "$2"); $worksheet->write("D$row", "$3"); ++$row; print "$4 $1, $2, $3\n"; } $line = ''; } close inFile; $workbook->close ();
Update: Fix write to spreed sheet (required quotes).

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: new : greping
by pingme8705 (Acolyte) on Sep 08, 2005 at 06:10 UTC
    hi father ! If the file is like this :
    logging to device1 is 2.3.5.1 . . . 56% /var 38% / 31% Interleaved device1 Tue Sep 9 11:26:44 ist 2005 logging to device2 is 2.3.5.1 . . . 96% /var 88% / 100% Interleaved device2 Tue Sep 5 10:26:44 GMT 2005 logging to device5-PPP is 2.3.5.1 . . . 156% /var 138% / 131% Interleaved device1 Tue Sep 9 11:26:44 ist 2005 logging to device8-PPm is 2.3.5.1 . . . 596% /var 688% / 100% Interleaved device2 Tue Sep 5 10:26:44 GMT 2005
    then the code doesn't shows the correct device name. it shows ...
    device1 56,38,31 device2 96,88,100 device1 156,138,131 device2 596,688,100
    and excel no change !

      Final modification to code for extended file syntax

      use warnings; use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("data.xls"); my $worksheet = $workbook->add_worksheet(); $worksheet->write ("A1", 'Device'); $worksheet->write ("B1", '/var'); $worksheet->write ("C1", '/'); $worksheet->write ("D1", 'interleaved'); my $row = 2; my $line = ''; my $device; open inFile, '<', 'data.txt' or die "Couldn't open data.txt: $!"; while (! eof inFile) { $line .= ' ' . <inFile>; chomp $line; next if ! ($line =~ /logging\sto\s(device.*?)\s+/i) and ! eof inFile +; my $nextDevice = $1; if ($line =~ /(\d+)%\s+\/var\s+(\d+).*?(\d+)%\s+Interleaved\s+(\w+)/ +i) { $worksheet->write("A$row", "$device"); $worksheet->write("B$row", "$1"); $worksheet->write("C$row", "$2"); $worksheet->write("D$row", "$3"); ++$row; print "$device $1, $2, $3\n"; } $device = $nextDevice; $line = ''; } close inFile; $workbook->close ();

      Perl is Huffman encoded by design.
        hi father !
        logging to device1 is 2.3.5.1 . . . 56% /var 38% / 31% Interleaved disks faults avm fre cs us sy id 504956 19880 32 3 24 72 device1 Tue Sep 9 11:26:44 ist 2005 logging to device2 is 2.3.5.1 . . . 96% /var 88% / 100% Interleaved disks faults avm fre cs us sy id 504956 19880 32 3 24 72 device2 Tue Sep 5 10:26:44 GMT 2005 logging to device5-PPP is 2.3.5.1 . . . 156% /var 138% / 131% Interleaved disks faults avm fre cs us sy id 504956 19880 32 3 24 72 device5-PPP Tue Sep 9 11:26:44 ist 2005 logging to device8-PPm is 2.3.5.1 . . . 596% /var 688% / 100% Interleaved disks faults avm fre cs us sy id 504956 19880 32 3 24 72 device8-PPm Tue Sep 5 10:26:44 GMT 2005
        if i want to take the avm fre id and its corresponding values and also the time in the text. wat should i do father ?
Re^2: new : greping
by pingme8705 (Acolyte) on Sep 08, 2005 at 05:48 UTC
    hi father ! In the excel sheet this was the output:
    Device /var / interleaved B2 B3
    but it gave correct output in the screen ( i mean the command prompt)