CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Stuck on how to search for elements in array against text file

 



asandybox
Novice

Nov 29, 2013, 9:29 PM

Post #1 of 8 (1372 views)
Stuck on how to search for elements in array against text file Can't Post

Hi all,

I'm stuck on figuring out how to extract content that matches each element of my array and then write the result to a file.

Here is code I've been working on:


Code
#!/usr/bin/perl 

use strict;
use warnings;

open(ACLSIN, "<:utf8", "sample.txt") || die ("Input not found!\n");

while(my $line = <ACLSIN>) {

my @aclgroups = split(" ", $line);
my $aclgroup = $aclgroups[1];

my @unique;
my %seen;

#file glob so we can output multiple array file handles.
local *FILE;

foreach my $acl($aclgroup) {
#use unique to remove remove dups.
if (! $seen{$acl}) {
push @unique, $acl;
$seen{$acl} = 1;

if ... grep?


#open(FILE, ">$acl.grp") || die;
}
}
}

close(ACLSIN);


Within sample.txt I have the following entries:


Code
access-list outervpn-in extended permit icmp host 9.10.1.196 host 7.8.8.9 
access-list outervpn-in extended permit icmp host 9.10.1.197 host 7.8.8.4
access-list internet-in extended permit icmp any host 10.109.90.131 unreachable
access-list internet-in extended permit icmp any host 10.109.90.132 unreachable
access-list 400 extended permit ip host 192.168.3.83 192.168.11.0 255.255.255.0
access-list 400 extended permit ip host 192.168.3.82 192.168.11.4 255.255.255.0
access-list inside-traffic extended permit tcp host 192.16.0.18 object-group webdevelopment eq 25
access-list inside-traffic extended permit tcp host 192.16.0.12 object-group webdevelopment eq 443
access-list webdmz-inside extended permit icmp host 192.168.1.2 host 192.168.3.1 echo-reply
access-list webdmz-inside extended permit icmp host 192.168.4.2 host 192.168.5.1 echo-reply


My goal was to open sample.txt, extract the second field which contains the "acl-groups" do a uniq, and load them into an array, then search each unique element against the original file and create individual group files ex:

webdmz-inside.grp
400.grp
outervpn-in.grp

these files would contain everything that matched from the unique array.

Ex:


Code
outervpn-in.grp 

access-list outervpn-in extended permit icmp host 9.10.1.196 host 7.8.8.9
access-list outervpn-in extended permit icmp host 9.10.1.197 host 7.8.8.4


The goal is to break out all access groups into separate files for easier parsing.

I'm stuck on the search each element of the array against the original array and write to my multiple file handles.

Any suggestions would be helpful.

Thanks


BillKSmith
Veteran

Nov 30, 2013, 6:39 AM

Post #2 of 8 (1347 views)
Re: [asandybox] Stuck on how to search for elements in array against text file [In reply to] Can't Post

As long as your data file is reasonably small, it would be much easier to read all your data into a hash-of-arrays.
Use the acl as the hash key. The value would be a reference to an array of lines.

Close the input file

For each key, open a file of that name and print the corresponding array to it. Close each file before opening the next.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Nov 30, 2013, 6:52 AM

Post #3 of 8 (1345 views)
Re: [asandybox] Stuck on how to search for elements in array against text file [In reply to] Can't Post

You should be using lexical vars for filehandles instead of barewords and you should be using the 3 arg form of open.

$aclgroup is a single item, not a list, so that foreach loop will only make 1 iteration.

Since @unique and %seen are declared inside the while loop, they will be reset (overwritten) for each line in the file.


Quote
#file glob so we can output multiple array file handles.
local *FILE;

I'm not sure what you intended this to do, but it doesn't do what the comment suggests.

Suggestions:
Specify a limit value of 3 in the split statement so that it will split the line into 3 values. Since the first field/column is not needed, you can either use undef as a placeholder or use an array slice to extract the 2nd and 3rd values.

Store the data in a HoA (hash of arrays) where the key is the 2nd field/column and the 3rd field is pushed onto the HoA.

After the "sample.txt" file has been processed and HoA built, loop over the hash keys and open the required filehandle and dump the corresponding array to that filehandle.


Kenosis
User

Nov 30, 2013, 12:02 PM

Post #4 of 8 (1321 views)
Re: [asandybox] Stuck on how to search for elements in array against text file [In reply to] Can't Post

Perhaps I'm missing something here, but if your goal is to group sample.txt entries into their own files based upon the entries' second field, consider the following:

Code
use strict; 
use warnings;

while (<>) {
my $file = (split)[1];
open my $fh, '>>', "$file.grp" or die $!;
print $fh $_;
}

Usage: perl script.pl sample.txt

Hope this helps!


(This post was edited by Kenosis on Nov 30, 2013, 12:42 PM)


asandybox
Novice

Nov 30, 2013, 4:19 PM

Post #5 of 8 (1301 views)
Re: [Kenosis] Stuck on how to search for elements in array against text file [In reply to] Can't Post

Holy moly batman, you did in 4 lines what took me 3 days of struggling to accomplish. Smacks head against table...

Your perl fu is strong, I don't know how you came up with this so I'll go over your perl.


(This post was edited by asandybox on Nov 30, 2013, 4:25 PM)


asandybox
Novice

Nov 30, 2013, 4:21 PM

Post #6 of 8 (1297 views)
Re: [BillKSmith] Stuck on how to search for elements in array against text file [In reply to] Can't Post

Yah the data file was a acl dump of a firewall, it was about 4000 lines. Need to do some reading on hashes of hashes.


asandybox
Novice

Nov 30, 2013, 4:24 PM

Post #7 of 8 (1294 views)
Re: [FishMonger] Stuck on how to search for elements in array against text file [In reply to] Can't Post

Fish monger, this was the best I could come up with to get multiple file's out from an array. I did some googling and ran into this

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

Which is where the madness came from.


FishMonger
Veteran / Moderator

Nov 30, 2013, 6:25 PM

Post #8 of 8 (1281 views)
Re: [asandybox] Stuck on how to search for elements in array against text file [In reply to] Can't Post

That perlmonks thread is almost 14 years old. Back then, which is considered ancient in terms of perl coding, it was probably ok and maybe even appropriate. However, with today's standards that would be considered funky if not poor coding practice.

The task you're needing to do in this script doesn't not need an array of filehandles, but if it did, the following would be a more appropriate method.

First I'm assuming that you built a hash similar to what I previously suggested. Also, instead of an array of filehandles, I'd use a hash of filehandles.


Code
my %filehandle; 

foreach my $key (keys %aclgroups) {
open $filehandle{$key}, '>' "$key.grp" or die "failed to create '$key.grp' <$!>";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives