Selecting Specific Columns and Insert the delimiter TAB

Hi,
I am writing a Perl Script for the below :

I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file.

For example:
Header Information:

Footer Information:

Data looks like:
Each line has around ~210 columns and is Pipe delimited.

The outfile should contain the lines with only specific Columns and should be TAB delimited.
Specific Columns:

So I have started writing the Perl script:

I am using array spice to eliminate the Header and footer information..Please correct me if I am wrong.

Now, Once I load the file into an array, how do I select the above selected columns and then insert the delimiter as TAB in Perl.

Would that be easier if I use hashes or array ?

Could someone Please help me out in this. Really appreciate your thoughts.

Hi,

Use 'split' to save all columns in an array and select them by index:

@fields = split /\|/, $_;
print join( "\t", @fields[1, 3, 6 .. 10, 12] ), "\n";

Regards,
Birei

1 Like

Hi birei,

Thanks a lot for your reply.

After using the "Split" function and then while saving the columns into an array,
I am struggling how to eliminate the first 231 lines and the last 4 lines, because these are the header information and footer information.

@array = <F>;
close F

my @fields;

foreach (@array) {

$line = $_;
@fields = split (/\|/, $line);

print OUT join( "\t", @fields[1, 3 .. 7, 10 ..13, 15 .. 33, 36, 37, 40, 55 .. 66, 68 .. 103, 104, 105 .. 107, 109 .. 128, 130 .. 137, 187] ), "\n";
}
close OUT;

how do I use the array splice while saving the column values into the array ?

@array[231..$#array-4];

Could someone please help me out to eliminate the Header and footer information while saving the data into the array ?

Would really appreciate your thoughts.

A slice of the array content should work:

@arr = @arr[231 .. $#arr - 4];

It seems you have code like that, take a look to this example:

$ cat infile
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
$ cat script.pl
use warnings;
use strict;

my @arr = <>;
print "Before -> ", "@arr";
@arr = @arr[3 .. $#arr-2];
print "After -> ", "@arr";
$ perl script.pl infile
Before -> Monday
 Tuesday
 Wednesday
 Thursday
 Friday
 Saturday
 Sunday
After -> Thursday
 Friday

Regards,
Birei

1 Like

Hi birei,

Thank a lot for your help.

I Really appreciate your time and concern. Thank you very much for your help.