http://www.perlmonks.org?node_id=1026251


in reply to Regex to extract certain lines only from command output/text file.

Hi Monks,

First of all, thank you very much for your help on this issue. I tried all the options above and they worked great. however, I wanted to crack the issue without using much of the techniques mentioned above, so I tried a different approach and it worked !!. What also helped is, this time, I could safely ignore the line that has blanks next to it...

So given below is the line of output.

C:\perlscripts>more 3par.txt Id Name Persona -WWN/iSCSI_Name- Port 2 hpux-host2 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 --- 1122334455667788 --- 1122334455667799 4:3:1 3 hpux-host3 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 --- 1122334455667788 --- 1122334455667799 --- 4 hpux-host4 HPUX-legacy 1122334455667788 --- 1122334455667799 3:3:1 1122334455667788 1:3:1 1122334455667799 4:3:1 5 hpux-host5 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 3:3:1 1122334455667788 1:3:1 1122334455667799 4:3:1 5 hpux-host6 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 3:3:1 1122334455667788 1:3:1 1122334455667799 4:3:1 7 hpux-host HPUX-legacy 1122334455667788 2:3:1 1122334455667799 3:3:1 1122334455667788 --- 1122334455667799 4:3:1

From this, I only had to take out those hosts which have one or more --- in the output. Here's my attempt:

#!/usr/bin/perl use warnings; use strict; my $space_sep_FH; my @para; open($space_sep_FH,'>', 'space_sep.txt') or die "Error:$!.$^E"; while (<>) { next if /Id Name Persona -WWN\/iSCSI_Name- Port/; s/(^[ 0-9])+/\n\n$1/; print $space_sep_FH "$_"; } print "Done writing to space_sep.txt.\n"; $/ = "\n\n"; open($space_sep_FH,'<', 'space_sep.txt') or die "Error:$!.$^E"; @para = <$space_sep_FH>; foreach my $para (@para) { print "$para" if $para =~ /---/; }

And here is the output!!

C:\perlscripts>perl test_3par_tp.pl 3par.txt Done writing to space_sep.txt. 2 hpux-host2 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 --- 1122334455667788 --- 1122334455667799 4:3:1 3 hpux-host3 HPUX-legacy 1122334455667788 2:3:1 1122334455667799 --- 1122334455667788 --- 1122334455667799 --- 4 hpux-host4 HPUX-legacy 1122334455667788 --- 1122334455667799 3:3:1 1122334455667788 1:3:1 1122334455667799 4:3:1 7 hpux-host HPUX-legacy 1122334455667788 2:3:1 1122334455667799 3:3:1 1122334455667788 --- 1122334455667799 4:3:1 C:\perlscripts>

It now feels better to crack it in another way !!

May be my newly bought T430 was an inspiration :)

Perl is really a great language, it lets you do things your way.

Perlpetually Indebted To PerlMonks

use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
http://dwimperl.com/windows.html is a boon for Windows.