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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

62 Files Into 1 1

Status
Not open for further replies.

klmc

Programmer
Nov 14, 2003
18
US
I have 62 files that I need to make one. The file names are listed in a file named exist.txt. I need to loop through all of those names and append their content into a main file named main.txt. Is there an easy way to do that?

Thanks in advance.
 
What flavor of OS?

[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
If you are using UNIX:

@fns = `cat exsist.txt`;

foreach $line (@fns) {
chomp $line;
`cat $line >> newfile.txt`;
}

would work



[Blue]Blue[/Blue] [Dragon]

If I wasn't Blue, I would just be a Dragon...
 
The following should work. There are probably shorter ways of doing it, certainly if you take operating system facilities (like [tt]cat[/tt] into account.)

[tt]# open exist.txt and read all the lines into @files
open FILES, &quot;<exist.txt&quot;;
my @files = <FILES>;
close FILES;

# open main.txt for writing (overwrites!)
open OUTFILE, &quot;>main.txt&quot;;

# for each file
foreach (@files) {
# chop off the newline
chomp;

# open it for reading
open FILE, &quot;<$_&quot;;

# go through line by line and write it out to our output file
while (<FILE>) {
print OUTFILE;
}

# close the file
close FILE;
}

# close our output file
close OUTFILE;[/tt]
 
Thanks so much rosenk. You saved me some time!!
 
In abscence of cat, File::Cat:
Code:
use File::Cat;

open LIST, 'exists.txt' or die $!;
open OUT, '>main.txt' or die $!;

while(<LIST>)
{
	chomp;
	cat($_, \*OUT);
}
close OUT;
close LIST;
TMTOWTDI

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top