txtファイルの列の最大値と最小値を変数として出力する方法

txtファイルの列の最大値と最小値を変数として出力する方法

txtファイルの列の最大値と最小値を変数として印刷するには?

abc.txt:

col1 col2
1    35
1    20
1    40
1    50

最大値と最小値を識別するコードを作成しました。

cut -f2 -d "" abc.txt|head -1
cut -f2 -d "" abc.txt|tail -1
v1 = 'echo <??>|cut -f2 -d "" abc.txt|head -1'
v2 = 'echo <??>|cut -f2 -d "" abc.txt|tail -1'

ただし、変数に入れることはできず、ループで使用する必要があります。私はすべての行を印刷しているので、読みながら行を書きません。

ベストアンサー1

これを行う方法はいくつかあります。 Ainar-GのようにAwkを使いましょう。

$ cat abc.txt 
col1  col2
1 35
1 20
1 40
1 50

以下は、「col」パターンを含む行を無視し、ファイルの2番目の列を取得して番号をソートします。

$ min=$(awk '!/col/ {print $2}' abc.txt | sort -nr | tail -1)
$ max=$(awk '!/col/ {print $2}' abc.txt | sort -nr | head -1)

$ echo "$min"
20

$ echo "$max"
50

おすすめ記事