Operating System - HP-UX
1752710 Members
5898 Online
108789 Solutions
New Discussion юеВ

concatenate multiple files skipping the first 20 lines in each file

 
SOLVED
Go to solution
cbungau
New Member

concatenate multiple files skipping the first 20 lines in each file

Hi,

I have to concatenate multiple (500) text files with different number of rows, but skipping the first 20 rows/lines in each file.

I would be very grateful for any suggestions.

Thank you,

Cristian
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: concatenate multiple files skipping the first 20 lines in each file

If you have a file that contains the list of files and it isn't more than the command line, you can do:
for file in $(< file-list); do
tail -n +19 $file
done > new-file

(You need to use ls to get the files, you can replace $() by something like $(ls *.txt).)

If 500 is too big:
while read file; do
tail -n +19 $file
done < file-list > new-file
James R. Ferguson
Acclaimed Contributor
Solution

Re: concatenate multiple files skipping the first 20 lines in each file

Hi Cristian:

One way is:

# cat ./mycat
#!/usr/bin/perl
use strict;
use warnings;
my $OLDARGV = '';
my $lines = 0;
while (<>) {
if ( $OLDARGV ne $ARGV ) {
$OLDARGV = $ARGV;
$lines = 0;
}
$lines++;
next unless $lines > 20;
close ARGV if eof;
print;
}

...run as :

# ./mycat file1 file2 file3... > file.out

Regards!

...JRF...
# ./
cbungau
New Member

Re: concatenate multiple files skipping the first 20 lines in each file

Thank you,

Yours,
Cristian

cbungau
New Member

Re: concatenate multiple files skipping the first 20 lines in each file

Thanks, your suggestions helped.

Also it seems that there is a trim (trim 1.0 version is the latest) package for linux that will remove the first lines from the files and then, cat will do the job.

Cheers,

Cristian