无法在-e中修改标量赋值中的print

时间:2013-10-31 08:10:27

标签: perl

我想要使用一些代码来查找已排序文件中的重复项。 (awk启发的)代码如下所示:

perl -wnla -e 'BEGIN {$previous = -1} $F[1] == $previous ? print $F[1] : $previous = $F[1]' ../VCF/FIN_20.vcf

不幸的是它给了我一个错误:

Can't modify print in scalar assignment at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

我需要做些什么才能让它发挥作用?

聚苯乙烯。该文件看起来像

20  5282284 rs73594467
20  5282299 rs148317959
20  5282336 rs927106

1 个答案:

答案 0 :(得分:6)

只需在最后一条指令周围添加一些表:

perl -wnla -e 'BEGIN {$previous = -1} $F[1] == $previous ? print $F[1] : ($previous = $F[1])' 
#                                                                here ___^              ___^
没有他们,就像是:

($F[1] == $previous ? print $F[1] : $previous) = $F[1]