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

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

Replies are listed 'Best First'.
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!';
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 /.*/.
    A reply falls below the community's threshold of quality. You may see it by logging in.
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"; } }