I need help trying to process many small files. I need to remove the first line (header date line) if it exists and then rename the file q_dat_20110816.out => q_dat_20110816.dat.
I figured out how to open the file and do the match and print out the line I need to remove.
Now I need to figure out how to remove that line and then rename the file altogether.
How would you approach this?
Test code:
#!/usr/local/bin/perl
use strict;
use warnings;
my $file = '/share/dev/dumps/q_dat_20110816.out';
$file = $ARGV[0] if (defined $ARGV[0]);
open DATA, "< $file" or die "Could not open '$file'\n";
while (my $line = <DATA>) {
$count++;
chomp($line);
if ($line =~m/(Data for Process Q)/) {
print "GOT THE DATE: --$line\n";
exit;
}
}
close DATA;
Sample file: q_dat_20110816.out
Data for Process Q, for 08/16/2011
Make Model Text
a b c
d e f
g h i
New file: q_dat_20110816.dat
Make Model Text
a b c
d e f
g h i
Here’s one way to do it: