Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Copy rows of file to new document

by vinoth.ree (Parson)
on Jan 24, 2013 at 07:27 UTC ( #1015096=note: print w/ replies, xml ) Need Help??


in reply to Copy rows of file to new document

Just giving you an idea

Assuming you files are text files

1.Opening the directory which contains your 300 files using the opendir() function
#!/usr/bin/perl use strict; use warnings; my $dir_path="Your Directory Path"; opendir (DIR, $tmp_dir) or die $!;

2.Open the destination file with the appending modeopen() function

open (MYFILE, '>>data.txt');

3.Reading each files using the readdir function. readdir returns the name of each file or directory in the opened directory in turn when used in scalar context, or a list of the names of all files and directories in that directory when used in list context.

while (my $file_name = readdir(DIR)) {print "$file_name\n";}

4.Within this loop open each file in read mode and do another loop to each all the content and check for whatever the word match you need with regular expression. If matches write that content into the "data.txt" file as  print MYFILE $file_content; Once each file completes close each file respectively  close (MYFILE);

use the function closedir function to close the opened directory


Comment on Re: Copy rows of file to new document
Select or Download Code
Re^2: Copy rows of file to new document
by phineas629 (Novice) on Jan 25, 2013 at 02:12 UTC

    I tried but I can't seem to get it to work. I'm really new to this all. If I can't figure it out then I'll have to start doing them manually.

    My data is located in the directory 'c:\incoming\temp'. I don't know if I should make a file or will the script make it for me? Any help will be appreciated, even if you have to write it out exactly for me. I know you guys have tried to make it as clear as possible but I'm really having such a difficult time. I have so much work today and the boss won't be paying me if I have to catch up on the weekend. I'm as desperate as one can be to get this thing to work. Once again, all the help is appreciated.

    #!/usr/bin/perl use strict; use warnings; my $dir_path="\Incoming\Temp"; opendir (Incoming\Temp, $tmp_dir) or die $!; open (MYFILE, '>>vehicles.txt'); while (my $file_name = readdir(Incoming\Temp)) {print "$file_name\n";} print MYFILE $file_content; close (MYFILE); CLOSEDIR;
      • my $dir_path="\Incoming\Temp";
        This is not doing what you think. The backslash has a special meaning in double quotes. Fortunately, you can use a normal slash /.
      • opendir (Incoming\Temp, $tmp_dir) or die $!;
        Where is $tmp_dir coming from? Do you mean $dir_path? Moreover, you cannot use a non-alphanumeric character in a dirhandle name. Remove the backslash. Even better - use a lexical dirhandle:
        opendir my $DIR, $dir_path or die $!;
      • open (MYFILE, '>>vehicles.txt');
        Not really an error, but can be improved: use a lexical filehandle, 3-argument version of open, and test for failure:
        open my $OUT, '>>', 'vehicles.txt' or die $!;
      • Also note that $file_content is empty and undeclared. You probably want to use it inside the while loop to populate the output file:
        while (my $filename = readdir $DIR) { open my $FILE, '<', "$dir_path/$filename" or die $!; while (my $content = <$FILE>) { print $OUT $content; } }
        BTW, have you noticed the directory path must be prefixed to the filename?
      • CLOSEDIR;
        Perl is case sensitive. The function is in lowercase: closedir. Also, give it an argument.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        I've given this a couple attempts.I'm really trying to read over the notes but am just plain lost. This is the error I got with the code:

        "my" variable $dir_path masks earlier declaration in same statement at parse.pl line 5. syntax error at parse.pl line 5, near "opendir" Can't use global $! in "my" at parse.pl line 5, near "die $!" syntax error at parse.pl line 7, near ") {" Global symbol "$file_content" requires explicit package name at parse.pl line 8. Execution of parse.pl aborted due to compilation errors.

        #!/usr/bin/perl use strict; use warnings; my $dir_path="c:/incoming/temp;" opendir my $DIR, $dir_path or die $!; open my $OUT, '>>', 'c:/vehicles.txt' or die $!; while (my $file_name = readdir($DIR) {print "$file_name\n";} print MYFILE $file_content; close (MYFILE)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-07-30 23:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls