Sonya777 has asked for the wisdom of the Perl Monks concerning the following question:
The script should take as an input all the files in one folder starting with file*, and sort the content of each one:#!/usr/bin/perl use strict; use warnings; open my $input, '<' or die "Unable to open input file: $!"; my @file = <$input>; my @sorted_file = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my ($x) = $_ =~ /VerNumber:\((\d+)/i; [$_, $x]; } @file; open my $output, '>' or die "Unable to open output file: $!"; print $output $_ for @sorted_file;
Then, as an output I would like for the script to create new folder in which it will place new files, with the sorted content, keeping the same file names.file1.txt file2.txt ... file1000.txt
I have made the following script. It does write the files in the output folder, keeping the same file names, but the sorting part is not working and I am getting the same files in the output (even though the sorting script is working fine as a standalone one). Any help?/sorted file1.txt -> /sorted/file1.txt file2.txt -> /sorted/file2.txt ... file1000.txt -> /sorted/file1000.txt
I am a beginner in Perl and any help would be more than welcome! Thank you in advance!#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $version="0.2"; my $files_match=""; my $files_dir=""; my $file_name=""; my $help_flag=""; my $version_flag=""; GetOptions( 'm|match=s' => \$files_match, 'd|directory=s' => \$files_dir, 'h|help' => \$help_flag, 'v|version' => \$version_flag, ); sub sorting { my @file = "$_"; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { my ($x) = $_ =~ /VerNumber:\((\d+)/i; [$_, $x]; } @file; print FILE $_; } if (($files_match ne "") and ($files_dir ne "")) { chdir("$files_dir") or die "$!"; opendir (DIR, ".") or die "$!"; my @files = grep {/$files_match/} readdir DIR; my $files_size = $#files + 1; my $index_file = 1; print "Files to process: $files_size\n"; close DIR; foreach (@files) { open(FILE, ">./sorted/$_.sort") or die $!; my @singlefile = $_; print "Processing $index_file of $files_size files: $_ +\n"; local @ARGV = @singlefile; while(<>){ sorting($_); } close(FILE); $index_file++; print "OK: Sorted @singlefile \n"; } } elsif ((!$help_flag) and (!$version_flag)){printHelp();}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use Schwartzian transform across multiple files
by choroba (Cardinal) on Sep 19, 2016 at 13:17 UTC | |
by Sonya777 (Novice) on Sep 19, 2016 at 13:59 UTC | |
|
Re: Use Schwartzian transform across multiple files
by Corion (Patriarch) on Sep 19, 2016 at 12:35 UTC | |
by Sonya777 (Novice) on Sep 19, 2016 at 12:56 UTC | |
|
Re: Use Schwartzian transform across multiple files
by Marshall (Canon) on Sep 19, 2016 at 20:24 UTC | |
|
Re: Use Schwartzian transform across multiple files
by Marshall (Canon) on Sep 19, 2016 at 22:29 UTC | |
by Sonya777 (Novice) on Sep 20, 2016 at 12:29 UTC |