2つの列名(列番号は異なる場合があります)に基づいてデータストリームをソートする方法は?

2つの列名(列番号は異なる場合があります)に基づいてデータストリームをソートする方法は?

次のようにAPIからデータストリームを取得します。

redID  blueID  whiteID  
1      22       2  
44     15       41  
2      15       15  
31     2       14 

私がしなければならないのは、これを分類してblueIDwhiteIDの場所に送ることだけです。しかし、どのくらいの列があるかは事前にはわかりません。私が確信しているのは、常に少なくとも2つの列があるということです。
したがって、希望の出力は次のようになります。

redID  blueID  whiteID  
31     2       14  
2      15      15  
44     15      41  
1      22      2 

awk列名に基づいてこのストリームを並べ替える方法はありますか?
私が探している唯一の答えは次の形式です。

inputStream | some operations | sortedInputStream

どんなアイデアがありますか?

ベストアンサー1

次のことができます。

 # get the header line from the file and split each header to a different line
 header=$(head -1 $file_name | tr ' ' '\n')
 # get the index/line number of the blueID
 blueID_index=$(echo "$header" | grep -n "blueID" | sed 's/:.*//')
 # same for whiteID
 whiteID_index=$(echo "$header" | grep -n "whiteID" | sed 's/:.*//')
 # now build the sort command with the indexes you just computed
 sort -k$blueID_index -k$whileID_index

おすすめ記事