in reply to Combine files with same extension in new file

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 $!;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Combine files with same extension in new file
by AhmedABdo (Acolyte) on Oct 01, 2015 at 10:22 UTC
    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';
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks for answering me. yes as you said, it takes the output.txt file and run it as input. I added unlink 'output.txt'; before the "for my $file (glob '*.tsv')" as you said, but this time nothing happened and even did not make output.txt file. Thanks
        This is great perl code to combine multiple files into one. After adding that extra line before GLOB, it work perfect!!! Thank you very much Choroba!! Roger