views:

188

answers:

2

I am trying to read a directory which consists of hundreds of files. For every file in the directory, I am supposed to access(edit) an another file and replace a particular string 'information=filename' in that file by file names 'information=' present in '/home/test/test_these_files' directory. This is followed by executing the file '/home/test/controlfile.txt'. I have written the following script and have tried to pass names of files as an array to replace (filenames in string). I know that wont work, but I am not sure what will work. Any suggestions? FYI: the program doesn't show any compilation error.

#!use/bin/perl
use strict;
use warnings;

my $find = 'information=filename';

opendir (DIR, "/home/test/test_these_files") || die "can't open directory";
my @files = grep(/.aa/, readdir(DIR));
print @files;
print "\n";
closedir(DIR);

foreach (@files) {
    my $replace = 'information=<@files>'; # want to replace with 'information=<filenames>
    #filenames are names of files present in the '/home/test/test_these_file' directory
    open (FILE, '/home/test/controlfile.txt') || die "cant open file \n";
    my @body = <FILE>;
    print @body;
    open (OUT, '>/home/test/controlfile.txt') || die "can't create file \n";
    foreach $_ (@body) {
        (s/$find/$replace/g);
        print OUT "$_";
    }
    'perl /home/test/controlfile.txt';
    close(FILE);
    close(OUT);
}
exit;
A: 

Your main error is probably in your shebang:

#!/usr/bin/perl

instead of

#!use/bin/perl

Also could you rephrase your question. It is really hard to understand what you are trying to say.

--Edit

I think I understand your question, you need to create a file which indexes all files existing in a particular directory.

Things to consider 1) whether you are looking for files named any character followed by aa or anything with an extension with .aa 2) whether you need to index files which don't exist

#!/usr/bin/perl

use strict;

$files_dir = '/home/test/test_these_files';
#$file_ext = '.*\.aa'; # Get files with extension of .aa
$file_ext = '.aa'; # Get files with any character followed by 'aa'
$index_file = '/home/test/controlfile.txt';

# Scan the directory
opendir(DIR, $files_dir) || die "can't open directory";
# Get "file.aa" or "{char}aa" ?
my @files = grep($file_ext, readdir(DIR));
closedir(DIR);

$index_string = '';
foreach $file (@files) {
    $index_string .= $file . ' ';
}
chomp($index_string);

# If needed prepend previous index
# ....

$index_string = 'information=' . $index_string;
open(FILE, '>', $index_file) || die "cant open file \n";
    print FILE, $index_string;
close(FILE);

Try that out. Good luck.

Ryan Schumacher
Thanks a ton! It worked. Was stuck on this for quite some time now.
shubster
+1  A: 

its not at all clear what your trying to do, but though reading and writing to the same file and some sketchy syntax it almost seems like this is what you want to do. Though where t he SEARCH and REPLACE are to come from isn't clear. If thats inside controlfile along with a filename then thats pretty simple to gather.

for FILE in `cat controlfile`
do
  perl -i -e 's/SEARCH/REPLACE/' ${FILE}
done
hpavc