Print file in particular order in bash

前端 未结 6 407
谎友^
谎友^ 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条回答
  •  礼貌的吻别
    2021-01-13 13:40

    Simply:

    #!/bin/bash
    for i in RAM Cache Spec Searchms; do
        echo "$i `cat file.txt  | grep $i | grep -Eo '[0-9]{1,9}' | tr '\n' ' '`" >> out.txt
    done
    

    You can change the order in line 2 ( for loop )

    Output:

    $ cat out.txt 
    RAM 456 184 149 
    Cache 142 417 149 413 
    Spec 152 172 
    Searchms 131 385 188 
    

提交回复
热议问题