
列Bの最大値を見つけたいそして列 B の値が最大値の 20% 以上であるすべての行を保持します。
入力データ
A B C D E
2 79 56 SD L
1 09 67 JK S
9 60 37 KD G
0 10 47 SO E
望ましい出力
A B C D E
2 79 56 SD L
9 60 37 KD G
使用してみましたawk 'BEGIN {max = 0} {if ($2>max) max=$2} END {if ($2 >= (0.1*max)) print}' file_in > file_out
が、ファイルの最後の行と思われるものだけが出力されます。
答え1
後で再度確認できるように、すべての行を配列に保存する必要がありますEND{ }
。または、ファイルを 2 回スキャンします。つまり、すべての値と行を保存します。
awk 'NR == 1 {header=$0; next} # save the header
{ lines[NR] = $0; values[NR] = $2; # save the line and 2nd field
if ($2 > max) max = $2; } # update max
END { print header; # in the end, print the header
for (i = 1 ; i <= NR ; i++) { # (we skipped line 0)
if (values[i] >= max * 0.2) # print lines where $2 was high enough
print lines[i]; } } ' file_in
答え2
とミラー:
1) データをきれいに印刷する:
$> mlr --from data --ipprint --otsv cat
A B C D E
2 79 56 SD L
9 60 37 KD G
2) フィールドの最大値を追加しB
ますB_max
。
$> mlr --from data --ipprint --otsv stats1 -a max -f B -s -F
A B C D E B_max
2 79 56 SD L 79.000000
1 09 67 JK S 79.000000
9 60 37 KD G 79.000000
0 10 47 SO E 79.000000
3) フィルター行B >= B_max * 0.2
:
$> mlr --from data --ipprint --otsv stats1 -a max -f B -s -F then filter '$B >= $B_max*0.2'
A B C D E B_max
2 79 56 SD L 79.000000
9 60 37 KD G 79.000000
4) そしてまたcut
去るB_max
:
$> mlr --from data --ipprint --otsv stats1 -a max -f B -s -F then filter '$B >= $B_max*0.2' then cut -x -f B_max
A B C D E
2 79 56 SD L
9 60 37 KD G