Print file in particular order in bash

前端 未结 6 444
谎友^
谎友^ 2021-01-13 13:22

I have file with content:

file.txt:

Iteration 1
RAM: +456ms
Cache: +142ms (total +417ms)

Iteration 2
Spec: +152ms
Cache: +149ms (total +413ms)

Iter         


        
6条回答
  •  旧时难觅i
    2021-01-13 13:45

    This might work for you (GNU sed):

    sed -E '/:/!d;s/.([0-9]+).*/\1/;H;x;s/((\n\S+) \S+)(.*)\2(.*)/\1\4\3/;h;$!d;x;s/.//' file
    

    Any lines other than those that contain a : are noise, so delete them.

    Remove all but the key, a space and the first set of integers from each line.

    Append the result to the hold space.

    Using pattern matching, gather up like keys data and retain in the hold space.

    Delete all but the last line.

    At the end of file, swap to the hold space, remove the introduced newline and print the result.

提交回复
热议问题