EXAMPLE: In case anyone needs to do something similar here is what I made. I will go back and change some var names so its more readable but you get the idea. I'd still like to know how to write to the first line of the file without having to make a duplicate using perl. If any one knows that be cool please post.
$ cat insertline.pl
#!/usr/local/bin/perl
#Dir where all host files are kept.
$dir1 = '/export/home/joe/scripts/testfiles/';
$dir2 = '/export/home/joe/scripts/testfiles/finalchanges/';
#Pull all files in dir to array.
opendir(DIR1, "$dir1") || die("Cannot open directory");
@dir1 = readdir(DIR1);
closedir(DIR1);
for $file(<@dir1>) {
if ($file =~ /insertline\.(.*$)/) {
$addtofile = uc("!$1\n");
$modfile1 = "$dir1$file";
$modfile2 = "$dir2$file";
open(FILE1, "$modfile1") || die("Cannot open file");
open(FILE2, ">$modfile2") || die("Cannot open file");
print FILE2 "$addtofile";
@copy_this = <FILE1>;
print FILE2 (@copy_this);
close (FILE1);
close (FILE2);
}
}
exit (0);
I guess I could dump thee current text file to an array like this and just write over the existing file instead of making a second file. I thought there was a cleaner way of doing it but whatever haha