CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
concatenate files

 



shezam
New User

Jun 18, 2009, 9:04 AM

Post #1 of 7 (5148 views)
concatenate files Can't Post

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!


perllearner
Novice

Jul 24, 2009, 6:00 AM

Post #2 of 7 (4979 views)
Re: [shezam] concatenate files [In reply to] Can't Post

Hi,

Even its a late response, i would like to share this program to you.

#!/usr/bin/perl
use strict;

my @file1=`cat /ahome/irsadmin/anbu/perlmechanic/file1`;
my @file2=`cat /ahome/irsadmin/anbu/perlmechanic/file2`;
my @combination1;
`rm -rf /ahome/irsadmin/anbu/perlmechanic/joinfile`;
open(OUTPUT,">>/ahome/irsadmin/anbu/perlmechanic/joinfile") || die "cannot create file";
for(my $i=0;$i<$#file1;$i++)
{
chomp $file1[$i];
chomp $file2[$i];
$combination1[$i]=join "", $file1[$i], "/del", $file2[$i];
chomp $combination1[$i];
#$combination2[$i]=join($combination1[$i],$file2[$i]);
print OUTPUT "$combination1[$i] \n";
}
close OUTPUT;


FishMonger
Veteran / Moderator

Jul 24, 2009, 7:16 AM

Post #3 of 7 (4975 views)
Re: [perllearner] concatenate files [In reply to] Can't Post

Why are shelling out to cat?

That script has to be one of the most inefficient method and doesn't accomplish the requirements.


KevinR
Veteran


Jul 24, 2009, 4:00 PM

Post #4 of 7 (4964 views)
Re: [FishMonger] concatenate files [In reply to] Can't Post


In Reply To
Why are shelling out to cat?

That script has to be one of the most inefficient method and doesn't accomplish the requirements.


Maybe the nick "perllearner" explains why. Wink
-------------------------------------------------


perllearner
Novice

Jul 24, 2009, 8:30 PM

Post #5 of 7 (4955 views)
Re: [KevinR] concatenate files [In reply to] Can't Post

Sorry if I had coded anything wrong. And since I had shell in my system I used this method. If anyone knows any alternate method for reading the contents of a file and storing it in an array, kndly post it so that it would be helpful.



Thanks..


ichi
User

Jul 25, 2009, 7:45 AM

Post #6 of 7 (4951 views)
Re: [shezam] concatenate files [In reply to] Can't Post


In Reply To
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!


Code
perl -ne '(eof)?print "$_\n/del\n":print' file1 file2



ichi
User

Jul 25, 2009, 7:48 AM

Post #7 of 7 (4950 views)
Re: [perllearner] concatenate files [In reply to] Can't Post


In Reply To
Sorry if I had coded anything wrong. And since I had shell in my system I used this method. If anyone knows any alternate method for reading the contents of a file and storing it in an array, kndly post it so that it would be helpful.



Thanks..


no , its not wrong to shell out and call these tools. BUT, since you are using Perl, which is a "powerful" language, all the capabilities of shell tools , Perl can provide. So why not do everything in Perl. it makes your Perl code portable to windows machines (etc) as well...i suggest you read up on Perl's doc to familiarize yourself more ...


(This post was edited by ichi on Jul 25, 2009, 7:49 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives