|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Concatenate files
I want write a small bit of perl to read two files, file1.txt and file2.txt and concatenate them and create and output them to files.gz.
I need to insert a delimiter "\del" between file1.txt and file2.txt. Any suggestions guys? |
|
#2
|
||||
|
||||
|
> Any suggestions guys?
Post some effort demonstrating what you know so far. If it's only as far as making a copy of a single file, then we could help with specific issues. If you can read 2 files, but can't get the delimiter to work, we could help with that as well. Maybe you can do everything except the .gz bit. As it stands, it looks like you're after a free answer on a plate, with no effort on your part.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
#3
|
|||
|
|||
|
Sorry dude.
open(FILE1,">>file1.txt" ) || die("Cannot Open File"); print FILE1 "\\del\n"; open(FILE2,">>file2.txt" ) || die("Cannot Open File");; close (FILE1); close(FILE2); 1. I know this works as in it inserts the delimeter, haven't obv concatenated files yet. Should i not read the file1.txt into an array and try find the last line, add delimiter and then try and append file2. 2. Then pipe this to a gzip file. |
|
#4
|
||||
|
||||
|
Your approach modifies one of the input files.
Try reading from both files (one after the other), and write to a third file, which is then gzipped. |
|
#5
|
||||
|
||||
|
Does it have to be done in Perl? Since you're using GZip, I'm assuming you're on a *nix platform. You could just use the following:
Code:
echo "\del" | cat filename1 - filename2 | gzip - > combined.gz
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#6
|
|||
|
|||
|
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>,$delimiter,<FILE2>,$delimiter,<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. |
|
#7
|
||||
|
||||
|
So you're not limited to two files then?
You've a couple of potential difficulties with this one: first of all you're creating the entire concatenated file in memory (in the @files array). Secondly, by using 'push', it'll likely be slower than just a straightforward assignment (as you're not adding to existing content in the array). Better to just do: Code:
my @files = <FILE3>, $delimiter, <FILE2>, $delimiter, <FILE1>; Thirdly, you're now limited to exactly three files. Depending on the exact nature of what you're trying to do, I'd general favour a more general approach that could deal with any number of files. Again using the command line, you can use a Perl one-liner and a pipe to achieve this: Code:
perl -ne 'print;print "\\del\n" if eof && !eof()' file1 file2 file3 | gzip - > output.gz |
|
#8
|
||||
|
||||
|
http://search.cpan.org/~nwclark/per...ib/File/Copy.pm might make life a little easier
__________________
--Ax without exception, there is no rule ... Heavy Haulage Ireland Targeted Advertising Cookie Optout (TACO) extension for Firefox The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones ![]() 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Concatenate files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|