Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Combine files with same extension in new file

by AhmedABdo (Acolyte)
on Oct 01, 2015 at 08:47 UTC ( [id://1143492]=perlquestion: print w/replies, xml ) Need Help??

AhmedABdo has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I have many txt files in one directory, and want combine them all in one new file but with excluding the header of each file. I know how to do it using linux command, but I want to do it in Perl. Thanks and I appreciate any help
  • Comment on Combine files with same extension in new file

Replies are listed 'Best First'.
Re: Combine files with same extension in new file
by choroba (Cardinal) on Oct 01, 2015 at 09:13 UTC
    Use open to open a file for reading or writing. Use glob if you need to expand wildcards. Use the diamond operator to read from a file. $. contains the current line number, so skip the first line in each file.
    #!/usr/bin/perl use warnings; use strict; open my $OUT, '>', 'output.txt' or die "output.txt: $!"; for my $file (glob '*.txt') { open my $FH, '<', $file or die "$file: $!"; while (<$FH>) { print {$OUT} $_ unless 1 == $.; } } close $OUT or die $!;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks, it works, but it overwrite many times and does not stop
        What do you mean? If I understand correctly, when you run it again, it finds output.txt created in the previous run as one of the inputs - that might be the problem. So, before doing glob,
        unlink 'output.txt';
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Combine files with same extension in new file -- oneliner
by Discipulus (Canon) on Oct 01, 2015 at 09:20 UTC
    perl is too fun!
    >cat one.lox header dat1 dat2 > > >cat two.lox header dat3 dat4 > > >perl -lne "BEGIN { @ARGV = map glob, @ARGV; } if ($. > 1){print} cl +ose ARGV if eof;" *.lox > outfile.log<P> > > >cat outfile.log dat1 dat2 dat3 dat4<P>

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      What about the header?


      holli

      You can lead your users to water, but alas, you cannot drown them.
Re: Combine files with same extension in new file
by hippo (Bishop) on Oct 01, 2015 at 08:53 UTC

    Some general tips:

    1. use strict
    2. use warnings
    3. run in taint mode (with -T) since you'll be writing to the filesystem.

    Other than that: design an algorithm first, then code it up. Have fun.

Re: Combine files with same extension in new file
by perlron (Pilgrim) on Oct 01, 2015 at 09:11 UTC

    ..but I want to do it in Perl

    Hi, As addendum to the above perfectly fine responses, i would also like to suggest you come up with a loose design for this solution
    If its just a P.o.C to test your Perl skills,its always good to start from scratch reading thru the links mentioned by the monks, focusing on File I/O operations, a great way to start learning perl.
    Please search on PM,or see this post How to combine multiple files together if you want industry grade program alternatives, though through the years, you will find, *there's more than one way to do it*.


    The Great Programmer is one who inspires others to code, not just one who writes great code
      Thanks, I am going to do it
Re: Combine files with same extension in new file
by Anonymous Monk on Oct 01, 2015 at 08:50 UTC
Re: Combine files with same extension in new file
by Anonymous Monk on Oct 01, 2015 at 09:15 UTC

    Depending on your use case, a simple one-liner may work:

    perl -lne 'print unless /header contents/' file1 file2 file3 > new_file

    if the header is varying but can be characterized by a known pattern, or

    perl -lne 'print if $. > 3 } continue { close ARGV if eof' file1 file2 file3 > new_file

    if the header has a fixed, known size (3 lines in this example).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1143492]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found