Linux

突出顯示小於最後一列中所有先前數字的每個數字

  • May 16, 2021

輸入:

network-snapshot-000000        time 6m 40s       fid50k_full 34.9546
network-snapshot-000201        time 6m 52s       fid50k_full 30.8073
network-snapshot-000403        time 6m 51s       fid50k_full 33.3470
network-snapshot-000604        time 6m 51s       fid50k_full 32.7172
network-snapshot-000806        time 6m 49s       fid50k_full 30.3764

輸出:

network-snapshot-000000        time 6m 40s       fid50k_full 34.9546
network-snapshot-000201        time 6m 52s       fid50k_full 30.8073*
network-snapshot-000403        time 6m 51s       fid50k_full 33.3470
network-snapshot-000604        time 6m 51s       fid50k_full 32.7172
network-snapshot-000806        time 6m 49s       fid50k_full 30.3764*
$ awk 'NR == 1 { min = $NF } ($NF < min) { min = $NF; $0 = $0 "*" }; 1' file
network-snapshot-000000        time 6m 40s       fid50k_full 34.9546
network-snapshot-000201        time 6m 52s       fid50k_full 30.8073*
network-snapshot-000403        time 6m 51s       fid50k_full 33.3470
network-snapshot-000604        time 6m 51s       fid50k_full 32.7172
network-snapshot-000806        time 6m 49s       fid50k_full 30.3764*

min如果我們目前正在讀取第一行 ( NR == 1) ,這會將找到的最小值 , 初始化為最後一列中的第一個值。然後,對於每個輸入行,如果最後一列中的值嚴格小於我們的min值,則替換該值並附加min目前行。*

然後無條件輸出每一行。

引用自:https://unix.stackexchange.com/questions/649962