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
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 $!;
| [reply] [d/l] |
|
Thanks, it works, but it overwrite many times and does not stop
| [reply] |
|
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';
| [reply] [d/l] [select] |
|
|
|
|
Re: Combine files with same extension in new file -- oneliner
by Discipulus (Canon) on Oct 01, 2015 at 09:20 UTC
|
>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.
| [reply] [d/l] |
|
| [reply] [d/l] |
Re: Combine files with same extension in new file
by hippo (Bishop) on Oct 01, 2015 at 08:53 UTC
|
Some general tips:
- use strict
- use warnings
- 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.
| [reply] [d/l] |
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
| [reply] |
|
Thanks, I am going to do it
| [reply] |
Re: Combine files with same extension in new file
by Anonymous Monk on Oct 01, 2015 at 08:50 UTC
|
I know how to do it using linux command, but I want to do it in Perl. Thanks and I appreciate any help Same way, open output file, then open each input file in turn, skip the header, print the rest to output file
see get n lines before or after a pattern, Re: modifying a file with regex!, perlintro, open, readline, rename, Re^5: Remove a Line, perlintro, open, close, readline, rename, Re: Rewriting into file, @ARGV, Getopt::Long, Re^2: Write to existing file with character insert
| [reply] |
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).
| [reply] [d/l] [select] |
|
|