Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

needing to zip files in a series that begin with the same IP address

by diamondsandperls (Beadle)
on Jul 27, 2012 at 20:10 UTC ( #984091=perlquestion: print w/ replies, xml ) Need Help??
diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:

Below is an update but not quite there trying to archive files that begin with the same ip address in a random series all files that begin with the same ip address archive them to the same archive with the archive named whatever the ip is .zip.

#!perl use strict; use warnings; use Modern::Perl; use IO::Compress::Zip qw(zip $ZipError) ; my %files; my @files = <*.txt *.docx>; while (<@filez>) { next unless /^(\d+.\d+.\d+.\d+)/; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { do { zip $ip => $_ } for @{ $files{$ip} }; }

Comment on needing to zip files in a series that begin with the same IP address
Download Code
Re: needing to zip files in a series that begin with the same IP address
by diamondsandperls (Beadle) on Jul 27, 2012 at 20:41 UTC
    i feel im getting closer i failed to mention i need the output files to be the ip address e.g. 192.168.1.1.zip.

    #!perl #use strict; use warnings; use IO::Compress::Zip qw(zip $ZipError) ; my @textfiles = <*.txt *.docx>; foreach my $textfile (@textfiles) { if ($textfile =~ /(\d+.\d+.\d+.\d+.*)/) { my $ip = $textfile =~ /(\d+.\d+.\d+.\d+).*/; my $files; push @{$files{$ip}}, $textfile; zip $textfile => $output or die "zip failed\n"; } }
Re: needing to zip files in a series that begin with the same IP address
by Riales (Hermit) on Jul 27, 2012 at 20:42 UTC
    You need to escape the . like this: \.. At the end of your regex, you're matching everything left with the /.*/.
      im not needing to escape i have the ip addresses and files i need to work with.
Re: needing to zip files in a series that begin with the same IP address
by Kenosis (Priest) on Jul 27, 2012 at 21:10 UTC

    Perhaps the following will assist you with your coding:

    use Modern::Perl; my %files; while (<DATA>) { next unless /^(\d+.\d+.\d+.\d+)/; chomp; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { do { say "Zip $_ -> $ip.zip" } for @{ $files{$ip} }; } __DATA__ 192.168.1.1somestring.txt 192.168.1.1anotherRANDOMstring.txt 192.168.1.1.somerandom.docx 192.168.1.3somestring.txt 192.168.1.3anotherRANDOMstring.txt 192.168.1.3.somerandom.docxx

    Output:

    Zip 192.168.1.1somestring.txt -> 192.168.1.1.zip Zip 192.168.1.1anotherRANDOMstring.txt -> 192.168.1.1.zip Zip 192.168.1.1.somerandom.docx -> 192.168.1.1.zip Zip 192.168.1.3somestring.txt -> 192.168.1.3.zip Zip 192.168.1.3anotherRANDOMstring.txt -> 192.168.1.3.zip Zip 192.168.1.3.somerandom.docx -> 192.168.1.3.zip

    Hope this helps!

      my apology the 192.168.1.1 is an example and it will be random so the ip is unknown

        It's OK if the IPs are random, as your regex will match any IPv4 address. Those in the data set are just for the example script. That is, you'll need to to code the grabbing of the actual file names.

Re: needing to zip files in a series that begin with the same IP address
by Kenosis (Priest) on Jul 28, 2012 at 02:33 UTC

    diamondsandperls,

    Perhaps our continued conversation got buried in the nested postings above. However, assembling the suggestions in those postings (plus a couple of minor tweaks) into a single script yields the following:

    use Modern::Perl; use IO::Compress::Zip qw(zip $ZipError); my %files; for (<*.txt *.docx>) { next unless /^(\d+.\d+.\d+.\d+)/; chomp; push @{ $files{$1} }, $_; } for my $ip ( keys %files ) { say 'Adding ' . @{ $files{$ip} } . " file(s) to archive $ip.zip"; zip \@{ $files{$ip} } => "$ip.zip" or die "zip failed: $ZipError\n +"; } say 'Done!';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984091]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-09-02 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls