concating files together

Andrew Wilson andrew at rivendale.net
Fri May 23 10:07:16 CDT 2003


On Fri, May 23, 2003 at 04:08:54PM +0200, D at vid lacravate wrote:
> Hi ,
> 
> En ce jour du Fri, 23 May 2003 14:46:27 +0100,
> Boyle Bernadette <B.Boyle at gcal.ac.uk> parlait ainsi :
> > Can anyone tell me id there is a Perl command to concat files into one. 
> > more /var/log/maillog* > /tmp/output
> I am not namely from Belfast so i hope my answering to you won't be seen as some st(om|ep)ping in ...

I don't mind, I doubt anyone else does.
 
> Now for your question :
> It depends what you want to do ... Is it in a script , and then you don't need to be concise , or is it from the command line and then :
> perl -ne'open W,"outputfile";print W $_ for @ARGV;close W;' file1 file2 file3...
> 
> Would probably do...

It 'would do' but since you're going to print every line anyway then why
would you use -n?  The -n says to perl loop over these but don't print
the result.  Then you have to tell perl to print to the W filehandle
that you've opened. If I was going to start opening files, I'd probably
do this instead:

  perl -p -e 'BEGIN{open STDOUT, ">/tmp/output"}' /var/log/maillog*

That avoids the shell redirection.  The BEGIN block is executed once
before the loop over the input, it closes and reopens the STDOUT to
point to the output file that we want and then loops over each line of
the files given on the command line and prints it to STDOUT.

While your version works, it seems a bit cluttered to me.  There Is, of
course, More Than One Way To Do It ;-)

andrew
--
Gemini: (May 21 - June 21) People will finally lose patience with you
this week when God announces He's been finished with you for years.



More information about the Belfast-pm mailing list