使用Perl脚本进行数据操作

时间:2014-05-12 03:50:20

标签: perl

输入:

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

预期输出

aaa  15  16
bbb   7   8
ccc  43  44

我知道要使用awk来解决这个问题,但我需要在Perl中执行此操作。我尝试在Perl中使用数组,如

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

我不知道如何将1添加到col2并将其作为col3。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:4)

在perl oneliner

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

如果要截断标题行:

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

如果要完全删除标题行:

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

答案 1 :(得分:0)

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