Perl将管道定界文件的前两行合并为一个管道定界线

时间:2018-10-17 12:41:01

标签: arrays perl join delimited

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

至:
time_stamp | cpu_util | mem_util |

使用perl,因为这需要在Windows和Linux上都可以使用

2 个答案:

答案 0 :(得分:0)

检查一下:

> 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|
>

答案 1 :(得分:0)

  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 |接口名称| NetworkSpeed | InKBRate | OutKBRate |