0

Trying to combine first 2 lines of a perl array as shown below:
time |cpu |mem|
stamp|util|util |

to:
time_stamp|cpu_util|mem_util|

Using perl as this needs to work on both windows and linux

AdamR
  • 1
  • 3
    Show how you tried to solve the problem and where you stucked – z0lupka Oct 17 '18 at 12:49
  • 2
    Where's your array? All you have shown us is text that looks like it might be from CSV files, with inconsistent white space. – simbabque Oct 17 '18 at 12:57
  • Sorry for the formatting issues. This is my first time to post. The output of a windows command is inputted into an array. The array would look something like this: – AdamR Oct 17 '18 at 13:10
  • What have you tried? What problems are you having? Show us your code. – Dave Cross Oct 17 '18 at 16:26

2 Answers2

0

Check this out:

> 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
  • 8,534
  • 3
  • 13
  • 38
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";  

output: TimeStamp|InterfaceName|NetworkSpeed|InKBRate|OutKBRate|

AdamR
  • 1