如何在awk中获取列名?

时间:2014-08-14 15:25:42

标签: linux shell awk header

我有一个以下格式的数据文件:

Program1, Program2, Program3, Program4
0,        1,        1,        0
1,        1,        1,        0

列是程序名称,行是程序的功能。我需要编写一个awk循环,它将遍历每一行,检查一个值是否等于1,然后返回列名并将它们放入“results.csv”文件中。所需的输出应为:

Program2, Program3
Program1, Program2, Program3

我正在尝试使用此代码,但它不起作用:

awk -F, '{for(i=1; i<=NF; i++) if ($i==1) {FNR==1 print$i>>results}; }'

非常感谢帮助!

3 个答案:

答案 0 :(得分:3)

awk -F', *' '
NR==1 {for(i=1;i<=NF;i++) h[i]=$i; next}
{
    sep="";
    for(x=1;x<=NF;x++) {
        if($x) {
            printf "%s%s", sep, h[x]; 
            sep=", ";
        }
    }
    print ""
}' file

输出:

Program2, Program3
Program1, Program2, Program3

答案 1 :(得分:1)

$ cat tst.awk
BEGIN { FS=", *" }
NR==1 { split($0,a); next }
{
    out = ""
    for (i=1; i<=NF; i++)
         out = out ($i ? (out?", ":"") a[i] : "")
    print out
}

$ awk -f tst.awk file
Program2, Program3
Program1, Program2, Program3

答案 2 :(得分:0)

我对事物的看法更加冗长,但应该处理尾随的逗号。不过,实际上不是一个单行。

BEGIN {                                                                     
    # Formatting for the input and output files.
    FS = ", *"
    OFS = ", "
}

FNR == 1 {
    # First line in the file
    # Read the headers into a list for later use.
    for (i = 1; i <= NF; i++) {
        headers[i] = $i
    }
}

FNR > 1 {
    # Print the header for each column containing a 1.
    stop = 0
    for (i = 1; i <= NF; i++) {
        # Gather the results from this line.
        if ($i > 0) {
            stop += 1
            results[stop] = headers[i]
        }
    }
    if (stop > 0) {
        # If this input line had no results, the output line is blank
        for (i = 1; i <= stop; i++) {
            # Print the appropriate headers for this result.
            if (i < stop) {
                # Results other than the last
                printf("%s%s", results[i], OFS)
            } else {
                # The last result
                printf("%s", results[i])
            }
        }
    }
    printf("%s", ORS)
}

将其保存为类似script.awk的内容,然后将其运行为:

awk -f script.awk infile.txt > results