Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collecting Data from Multiple Files

Status
Not open for further replies.

SensesFail

Programmer
Dec 9, 2008
3
US
Hi,

I have 500 different data files that each contain one line of text. I would like to open each file, grab the text and write it to another file, so that when I am finished, I will have all the lines of data stored in one file.

Does anyone know of a way to accomplish this using Perl?

Thank you!
 
Is there anyway to get the filenames into a list? Like are they all in one directory or do they all have a common identifier in the name or file extension? If so:

Code:
@ARGV = (all the filenames);
open(OUT, ">>", 'path/to/file') or die "$!";
while(<>){
   print OUT "$_\n";
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
All files are in the same directory, and they are named as output0.txt, output1.txt, ..., output499.txt

Thanks for the code KevinADC! I just have a few questions: Do I need to pass the name of each file to the program from the command line? And could you please briefly describe what the while loop is doing?

Thanks again!
 
You don't need to pass any filename to the script unless you wanted to look for the file in a different directory or change a search pattern or something else. As lons as you will always read from the same directory and the filenames remain consistent just call the script with no arguments:

Code:
use strict;
use warnings;
chdir('path/to/start/dir) or die "Can't chdir: $!"; 
@ARGV = glob("output*.txt");
open(OUT, ">>", 'path/to/file') or die "$!";
while(<>){
  print OUT "$_\n";
}
close OUT;
print "Finished\n";
exit();

If the input files have a newline on the end drop the \n in the print line;

The while(<>) construct opens each filename in the @ARGV array and reads each line in the file into $_.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this line:

Code:
chdir('path/to/start/dir) or die "Can't chdir: $!";

is missing a quote after dir, should be:

Code:
chdir('path/to/start/dir') or die "Can't chdir: $!";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top