如何根据其他两列的唯一组合从列中提取组

如何根据其他两列的唯一组合从列中提取组

我有这样的数据:

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

如何根据其他两列形成的组的组合迭代地从第一列中提取样本,以便获得样本 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

为了获得第二列和第三列的独特组合,但我似乎无法对此做任何事情,特别是拉动第一列,这正是我所需要的。

答案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

相关内容