Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 19th, 2009, 09:36 AM
shezam shezam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 3 shezam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 22 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old June 19th, 2009, 09:47 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,139 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 1 Day 23 h 22 m 21 sec
Reputation Power: 861
> 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.

Reply With Quote
  #3  
Old June 19th, 2009, 09:58 AM
shezam shezam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 3 shezam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 22 sec
Reputation Power: 0
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.

Reply With Quote
  #4  
Old June 19th, 2009, 10:06 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,139 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 1 Day 23 h 22 m 21 sec
Reputation Power: 861
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.

Reply With Quote
  #5  
Old June 19th, 2009, 10:22 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,986 ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 1 Day 15 h 32 m 11 sec
Reputation Power: 2212
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

Reply With Quote
  #6  
Old June 22nd, 2009, 05:15 AM
shezam shezam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 3 shezam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 22 sec
Reputation Power: 0
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.

Reply With Quote
  #7  
Old June 22nd, 2009, 06:23 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,986 ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level)ishnid User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 1 Day 15 h 32 m 11 sec
Reputation Power: 2212
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

Reply With Quote
  #8  
Old June 22nd, 2009, 08:14 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Dev Shed God 15th Plane (12000 - 12499 posts)
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,277 Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)Axweildr User rank is General 56th Grade (Above 100000 Reputation Level)  Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1Folding Points: 143856 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 1 Week 3 Days 13 h 14 m 41 sec
Reputation Power: 4721
Send a message via Google Talk to Axweildr
Orkut
__________________
--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 ...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Concatenate files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT