Imane
Imane

Reputation: 23

Deleting columns at specific position using perl

I am new to Perl and I am trying to delete columns that are defined in specific positions. My file looks like this:

https://i.sstatic.net/18l5a.png

knowing that the columns are numerated as following:

1-3 , 5-7 , 9-12 , 14-16 , 18-21 , 23-25 , 27-29 , 31-37 , 39-68 , 70-72 , 74

And I want to delete the columns that start at:

1-3 , 14-16 , 23-25 , 27-29 , 31-37 , 39-68 , 70-72 , 74

So that I can get this, only the columns: 5-7 and 9-12 but change their positions to: 1-5 and 7-10

I aslo want to add new columns at specific positions, (12-21) for example and get the following resulting file:

https://i.sstatic.net/eQ0Yp.png.

Thank you in advance!

Upvotes: 0

Views: 138

Answers (1)

Håkon Hægland
Håkon Hægland

Reputation: 40778

One approach is to use unpack. Try this perl p.pl < in.txt, where p.pl is:

while (<>) {
    my @cols = unpack "A3xA3xA4xA*", $_;
    CORE::say join ' ', $cols[2] . '  ', $cols[1], "      same";
}

Update:

According to discussion in the comments, the following solution solved the problem:

while (<>) {
    my @cols = unpack "A3xA3xA4xA*", $_;
    printf "%-5s %-5s %-4s\n", $cols[1], $cols[2], "same";
}

Upvotes: 1

Related Questions