我有一个格式如下的文件:
instance1 field1
instance1 field2
instance2 field1
instance2 field2
instance3 field1
instance3 field2
...
我想要一个 oneliner,这样它就可以转换为:
instance1 field1 field2
instance2 field1 field2
instance3 field1 field3
等等
我可以编写以下类型的 perl 脚本:
while (<STDIN>)
{
$line = $_;
$line2 = <STDIN>;
chomp $line; chomp $line2;
print "$line $line2\n";
}
但我宁愿有一个单行类轮。有什么想法吗?
最佳答案
使用行号 $.
的两种解决方案:
perl -lane 'print $. % 2 ? @F[0,1] : " $F[1]\n"' file
或者
perl -pe '$. % 2 ? chomp : s/\S*//' file
关于linux - 如何采用成对的线并连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23483236/