1

I've been trying to assign multiple files to a single file-handle. And from there process the entire filehandle to look for a certain pattern and then write that to FIN_RESULTS.

Linux
exp files
my/dir1/RESULTS
my/dir2/RESULTS
etc.

Here was my awkward fumble of an attempt

   open (FIN_RESULTS, ">", "FIN_RESULTS.txt") or die $!;

   open(RESULTS, "-|"," find my/ -name RESULTS -print0 | xargs -0 cat");
   while(<RESULTS>){
        if(/match_something/){
           do some commands;
           print FIN_RESULTS $_;
         }
close FIN_RESULTS;
close RESULTS;

But I just end up overwriting the perl script itself with the ls of the current directory.

Thanks for the help!!!!! I was able to implement what I originally had in mind.

I have a second question is it possible to implement the find in such a way that it looks through certain directories only? Such as if I have
my/abc_dir1/RESULTS
my/dsa_dir2/RESULTS
my/afx_dir3/RESULTS

but I only want to search through dir2 and dir3.

@array1 = qw( dir2 dir3);

foreach $array1(@array1)
{ open(RESULTS, "-|"," find my/*$array1 -name RESULTS -print0 | xargs -0 cat");
}

but I get this error, xargs: cat: terminated by signal 13. So it's not allowed to reopen a filehandle multiple times. Any suggestions as to what I can do?

user1539348
  • 513
  • 1
  • 7
  • 20
  • Can you be clearer about what you are trying to accomplish? I'm not sure what "open a file handle[r] with multiple files" means. What are you expecting this script to do? – Andy Ross Aug 14 '12 at 23:55
  • It's not clear to me what you're trying to do. Could you clarify? A bulleted step-by-step description (i.e. not code) would be helpful. – David Aug 15 '12 at 00:00

2 Answers2

3

Presuming you really want to do the external find:

open RESULTS, "-|", "find my -name RESULTS -print0 | xargs -0 cat"

Should do what you want.

Andy Ross
  • 11,699
  • 1
  • 34
  • 31
  • 1
    Thank you for the help. I was just wondering why "-|" had to be seperated by a comma. And I think I understand the xargs function. It's used to delimit blanks and newlines from the find command... is that correct? – user1539348 Aug 15 '12 at 15:46
3

There are two formats for the open function in Perl:

The two argument call:

open (RESULTS, "|find my/ -name 'RESULTS'")

The two argument call is simple, but if you happen to have a file name that has starts with a vertical pipe, you'd have problems.

The other is the preferred THREE ARGUMENT syntax:

open (RESULTS, "-|", "find my/ -name 'RESULTS'")

This syntax uses the second argument to state that type of open you're doing:

">" -  Write
">>" - Append
"<"  - Read
"-|" - Read from command
"|-" - Write to command

It looks like you've gotten the two confused.


Just a note:

You can do your find instead of Perl using the File::Find module.

It's more efficient since you're not firing off a separate process and more operating system independent since it'll work the same way on Windows or Linux.

David W.
  • 105,218
  • 39
  • 216
  • 337