Print file in particular order in bash

前端 未结 6 411
谎友^
谎友^ 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

    One using awk:

    $ awk '
    $1 !~ /^(|First)$/ {            # avoid forbidden keywords and empty lines
        gsub(/[^0-9]/,"",$2)        # remove non-numerals
        a[$1]=a[$1] OFS $2          # append to associative array 
    }
    END {                           # in the end
        for(i in a)                 # loop all keywords
            print i a[i]            # output
    }' file
    

    Output lines in awk default order (appears random):

    Cache: 142 149
    Searchms: 131 188
    Spec: 152 172
    RAM: 456 184 149
    

提交回复
热议问题