Perl将前两行Pipe分隔文件合并为一个管道分隔行

arrays perl join delimited

43 观看

2回复

1 作者的声誉

尝试组合perl数组的前两行,如下所示:
time | cpu | mem |
stamp | util | util |

to:
time_stamp | cpu_util | mem_util |

使用perl这需要在windows和linux上工作

作者: AdamR 的来源 发布者: 2018 年 10 月 17 日

回应 (2)


0

5376 作者的声誉

看一下这个:

> cat cpu.txt
time|cpu|mem|
stamp|util|util|
> perl -F'/\|/' -lane ' { push(@line,@F)} END { for($i=0;$i<3;$i++) { printf("%s", "${line[$i]}_${line[$i+3]}|") }print}' cpu.>
time_stamp|cpu_util|mem_util|
>
作者: stack0114106 发布者: 17.10.2018 12:59

0

1 作者的声誉

  For a newbie, this is what I came up with, but I think there is probably a more elegant solution. 



    @netifdata_array = (      Time|Interface    |     Network|         In|        Out|,
                                  Stamp|Name   |       Speed|    KB Rate|    KB Rate|);
    my  (@line1, @line2, @line3);
    my $ct;
    @line1 = split(/\s*\|\s*/, @netifdata_array[0]);                                 
    @line1 = grep(s/\s*//g, @line1);
    @line2 = split(/\s*\|\s*/, @netifdata_array[1]);
    @line2 = grep(s/\s*//g, @line2);
    $ct = $ct + scalar(@line2);
        for (my $i = 0; $i < $ct; $i++)
         {
            push (@line3, $line1[$i], $line2[$i], "|");
         }
          print @line3,"\n";  

输出:TimeStamp | InterfaceName | NetworkSpeed | InKBRate | OutKBRate |

作者: AdamR 发布者: 18.10.2018 05:58
32x32