Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Regex to extract certain lines only from command output/text file.

by perl514 (Pilgrim)
on Mar 30, 2013 at 08:24 UTC ( [id://1026251]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1026251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found