他の 2 つの列の一意の組み合わせに基づいて列からグループを取得する方法

他の 2 つの列の一意の組み合わせに基づいて列からグループを取得する方法

次のようなデータがあります:

Sample_1    Apples  Red
Sample_2    Apples  Red
Sample_3    Apples  Red
Sample_4    Apples  Red
Sample_5    Apples  Red
Sample_6    Apples  Green
Sample_7    Apples  Green
Sample_8    Apples  Green
Sample_9    Apples  Green
Sample_10   Apples  Green
Sample_11   Apples  Yellow
Sample_12   Apples  Yellow
Sample_13   Apples  Yellow
Sample_14   Apples  Yellow
Sample_15   Apples  Yellow

他の 2 つの列から形成されたグループの組み合わせに基づいて最初の列からサンプルを繰り返し取得し、サンプル 1 ~ 5、6 ~ 10、および 11 ~ 15 を取得するにはどうすればよいでしょうか。

最終的にやりたいことは、サンプルのリスト(上記のグループなど)を別のコマンドへの入力として取得することです。例:

comm -23 <(sort <all_samples.txt>) <(sort <[input from above]>) > <difference.txt>

私が試してみました:

awk '{print $2"\t"$3}' <file.txt> | uniq

2 列目と 3 列目の一意の組み合わせを取得しますが、これを使用して何もできないようです。特に、必要な最初の列を取得することはできません。

答え1

これがあなたがやろうとしていることですか?

$ awk '{vals[$2 FS $3] = vals[$2 FS $3] OFS $1} END{for (key in vals) print key vals[key]}' file
Apples Red Sample_1 Sample_2 Sample_3 Sample_4 Sample_5
Apples Green Sample_6 Sample_7 Sample_8 Sample_9 Sample_10
Apples Yellow Sample_11 Sample_12 Sample_13 Sample_14 Sample_15

それともこれでしょうか?

$ awk -v fruit='Apples' -v color='Green' '($2==fruit) && ($3==color)' file
Sample_6    Apples  Green
Sample_7    Apples  Green
Sample_8    Apples  Green
Sample_9    Apples  Green
Sample_10   Apples  Green

答え2

これは、入力を解析し、ニーズに合うと思われるデータの転置を出力する gawk スクリプトの簡単な例です。

#!/usr/bin/gawk -f

# Checks if type (column 2) or subtype (column 3) are 
# different from previous line.
(type != $2) || (subtype != $3) {
    # Prints the start of a new output line.
    # The NR!=1 check avoids that a new line is 
    # printed on the first line.
    printf("%s%s\t%s\t", (NR!=1)?"\n":"", $2, $3);
    type=$2;
    subtype=$3
}
{
    # Prints all sample (column 1) values on the 
    # current output line.
    printf("\"%s\" ", $1);
}
# prints a new line at the end of file.
END{
    print "";
}

の出力はscript.awk < input.lst次のようになります。 はscript.awk前のスクリプトで、input.lstは入力例です。

Apples  Red     "Sample_1" "Sample_2" "Sample_3" "Sample_4" "Sample_5" 
Apples  Green   "Sample_6" "Sample_7" "Sample_8" "Sample_9" "Sample_10" 
Apples  Yellow  "Sample_11" "Sample_12" "Sample_13" "Sample_14" "Sample_15" 

スクリプトの出力は次のように簡単に操作できます。

script.awk < input.lst | while read TYPE SUBTYPE LIST
do 
    echo $TYPE
    echo $SUBTYPE
    for ITEM in $LIST
    do  
        echo execute some command on $ITEM where type is $TYPE and subtype is $SUBTYPE
    done 
done

このスクリプトは非常に大まかであることに注意してください。たとえば、エラー処理はなく、入力内の空白や特殊文字のチェックもありません。

答え3

以下のスクリプトを試したところ、問題なく動作しました

for i in "Apples"; do for j in "Red" "Green" "Yellow"; do awk -v i="$i" -v j="$j" 'BEGIN{print "Below are table contains" " " i " and " " " j}$2==i && $NF==j{print $0}' filename; done; done

出力

Below are table contains Apples and  Red
Sample_1    Apples  Red
Sample_2    Apples  Red
Sample_3    Apples  Red
Sample_4    Apples  Red
Sample_5    Apples  Red
Below are table contains Apples and  Green
Sample_6    Apples  Green
Sample_7    Apples  Green
Sample_8    Apples  Green
Sample_9    Apples  Green
Sample_10   Apples  Green
Below are table contains Apples and  Yellow
Sample_11   Apples  Yellow
Sample_12   Apples  Yellow
Sample_13   Apples  Yellow
Sample_14   Apples  Yellow
Sample_15   Apples  Yellow

関連情報