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

答え2

コメントやその他のアイデアソースのおかげで、ついにこのコードを書いて私の質問に答えることができました。

   inputStream | awk -F'\t' -v OFS="\t" '{
            if ( col1 == ""){
                for (i=1;i<=NF;i++){
                    if ($i == "BlueId"){
                        col1=i;
                    }
                    else if ($i == "WhiteId"){
                        col2=i;
                    }
                }
            print "-1" "\t" "-1" "\t" $0
            }
            else {
                print $col1 "\t" $col2 "\t" $0
            }
        }' | sort -k1,1n -k2,2n | cut -f3- | outputStream

これは次のように機能します。ストリームデータを取得し、必要な列番号を見つけ、各行の前にソートに必要な2つの値を印刷します。次に、最初と2番目の列をソートして削除します。ありがとうございます!

関連情報