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!

Concatenate files

Status
Not open for further replies.

shezam

Programmer
Jan 30, 2009
11
IE

I want to write a little piece of perl that will take file1.txt, and file2.txt and concatenate them using /del as the delimiter.

So how would i do this?, read file1.txt, append /del at after last line and then append file2.txt after that.

All suggestions appreciated! Thanks!
 
Hi

This question was answered before, you can search this forum for solutions. Or you can implement it like this :
[ul]
[li]open both input files[/li]
[li]open the output file[/li]
[li]do a loop until any or both files end [red]*[/red][ul]
[li]read the next line from both files[/li]
[li]replace the end-of-line mark with '/del' at the end of the line read from the first file[/li]
[li]write both lines to the output file[/li][/ul][/li]
[li]close both input files[/li]
[li]close the output file[/li]
[/ul]
[red]*[/red] - You not specified if the files may have different length and if yes how that case should that be handled.

Feherke.
 
Can the output be a gzipped file, so concat file1.txt and file2.txt and create files.gz.
 
Ok here goes;

open(FILE1,"file1" ) || die("Cannot Open File1");
open(FILE2, "file2" ) || die("Cannot Open File2");
open(FILE3, "file3" ) || die("Cannot Open File3");
my @files;
my $delimiter = "\del\n";
push(@files, <FILE3>,$test,<FILE2>,$test,<FILE1>);
close (FILE1);
close (FILE2);
close (FILE3);

Do i have to write this to a third file -then gzip, or can I not just send the concatenation of this 3 files to a .gz file.
 
Something like this perhaps?

Code:
open(FILE1, "file1" ) || die("Cannot Open File1");
open(FILE2, "file2" ) || die("Cannot Open File2");
open(FILE3, "file3" ) || die("Cannot Open File3");
my @files;
my $delimiter = "\del\n";
push(@files,<FILE3>,$delimiter,<FILE2>,$delimiter,<FILE1>);
close (FILE1);
close (FILE2);
close (FILE3);
open(GZIP, "|gzip > output.gz") || die("Cannot launch gzip");
print GZIP @files;
close(GZIP);

A loop through the files would be neater of course, especially if there are more than 3...

Annihilannic.
 
open(GZIP, "|gzip > output.gz") || die("Cannot launch gzip");
print GZIP @files;
close(GZIP);

This doesn't appear to compress the data into a file. Is there any way i can may doing it using a command enclosed in backticks
 
Seems to work for me:

Code:
$ cat file1
Tue Jun 23 12:28:48 EST 2009
$ cat file2
Tue Jun 23 12:28:51 EST 2009
$ cat file3
Tue Jun 23 12:28:53 EST 2009
$ cat concat
open(FILE1, "file1" ) || die("Cannot Open File1");
open(FILE2, "file2" ) || die("Cannot Open File2");
open(FILE3, "file3" ) || die("Cannot Open File3");
my @files;
my $delimiter = "\del\n";
push(@files,<FILE3>,$delimiter,<FILE2>,$delimiter,<FILE1>);
close (FILE1);
close (FILE2);
close (FILE3);
open(GZIP, "|gzip > output.gz") || die("Cannot launch gzip");
print GZIP @files;
close(GZIP);
$ perl concat
$ ls -l output.gz
-rw-r--r--   1 username   users           63 Jun 26 10:46 output.gz
$ gunzip -c output.gz
Tue Jun 23 12:28:53 EST 2009
del
Tue Jun 23 12:28:51 EST 2009
del
Tue Jun 23 12:28:48 EST 2009
$

What happens when you do it?

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top