linux - 如何采用成对的线并连接起来?

标签 linux perl shell text

我有一个格式如下的文件:

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/

相关文章:

linux - 使用 alt+f2 启动 shell 脚本 (debian/mate)

c - 如何将 char 数组转换为 C 中的函数指针?

linux - 将文件从本地机器移动到集群

linux screen 分离和注销

c++ - 如何检查给定的 UTC 偏移量是否启用了夏令时?

regex - 匹配反斜杠后跟转义字符

perl - $@ 和 $! 之间的区别在 perl 中

perl - 密码中的特殊字符导致 Mojolicious UA 中的基本身份验证失败

linux - 在文件夹中的 gzip 文件中查找字符串

linux - qstat 和长作业名称