sara
sara

Reputation: 93

Data manipulation using Perl script

Input:

Col1 col2 col3 col4
aaa   15   23   A
bbb    7    5   B
ccc   43   10   C

Expected output

aaa  15  16
bbb   7   8
ccc  43  44

I know to get this using awk but I need to do this in Perl. I tried using an array in Perl like

 push(@output_array, $temp_array[0] . "\t" . $temp_array[1] .  "\n");

I don't know how to add 1 to the col2 and make it as col3. Can anybody help me out?

Upvotes: 0

Views: 853

Answers (2)

sara
sara

Reputation: 93

push(@output_array, $temp_array[0] . "\t" , $temp_array[1] . "\t" , $temp_array[1] + 1 . "\n");

Upvotes: 0

Miller
Miller

Reputation: 35208

In a perl oneliner

perl -lane 'print join("\t", @F[0,1], $F[1] + 1)' file.txt

If you want to truncate a header row:

perl -lane 'print join("\t", @F[0,1], $. == 1 ? $F[2] : $F[1] + 1)' file.txt

If you want to completely remove a header row:

perl -lane 'print join("\t", @F[0,1], $F[1] + 1) if $. > 1' file.txt

Upvotes: 4

Related Questions