2

I have a pretty specific question, and have been able to find lots on conditional subsetting using awk but none that lends enough explicit code for me to generalize to my situation. I have a file 'keys' and a file 'features'. The 'keys' table contains two variables, KEY and GROUP, toy example below.

    KEY        GROUP
    ---        -----
    1          GROUP0
    2          GROUP0
    3          GROUP1
    4          GROUP1
    5          GROUP2
    6          GROUP2  

The file 'features' contains a list of features of widgets like so

    ID          FEATURE      VALUE
    --          -------      -----
    A           num_user     10
    A           KEY          4
    B           num_user     2
    B           KEY          2
    B           battery      Large
    C           num_user     10
    C           KEY          15
    D           num_user     2
    D           KEY          2
    D           battery      Small
    E           num_user     2
    E           KEY          7
    E           battery      Small

I am trying to select all rows for an ID which has a value of 'KEY' that is in the 'KEY' column of 'keys' for a hardcoded list of 'GROUP' values. The desired result is

    ID          FEATURE      VALUE
    --          -------      -----
    A           num_user     10
    A           KEY          4
    B           num_user     2
    B           KEY          2
    B           battery      Large
    D           num_user     2
    D           KEY          2
    D           battery      Small

Any ideas?

1

Try this:

$ awk '
    FNR == NR {if (FNR > 2) { k[$1] } next}
    FNR > 2 {
        id[$1] = ($1 in id) ? id[$1] = id[$1]"\n"$0 : id[$1] = $0;
        if ($2 ~ /KEY/) { v[$1] = $3 };
        next;
    }
    1
    END {
        for (val in v) {
            if (v[val] in k) {
                print id[val];
            }
        }
    }
' keys features
ID          FEATURE      VALUE
--          -------      -----
A           num_user     10
A           KEY          4
B           num_user     2
B           KEY          2
B           battery      Large
D           num_user     2
D           KEY          2
D           battery      Small
| improve this answer | |
  • I'm very, very new to awk, is there any chance of you walking me through some of the code? Or direct me to a comprehensive reference resource? Thanks in advance! – mlegge Jun 10 '14 at 22:02
  • @McClellandKemp: You can refer to this:gnu.org/software/gawk/manual/gawk.html or shop.oreilly.com/product/9781565922259.do. But man awk is a good start, too. – cuonglm Jun 11 '14 at 2:50
  • I created these toy files as space delimited files (without headers) and then called the script, which ran without error but only printed the first block that matched: A num_user 10 A KEY 4... I'm unable to figure out where this can be fixed, do you see what might have caused it? @Gnouc – mlegge Jun 11 '14 at 18:06
  • @McClellandKemp: You must include the header. I tested with your input in question. – cuonglm Jun 11 '14 at 18:17
  • Thank you very much! Out of curiosity, how could you perform the exact procedure without the column headers? And how does it recognize the columns from the first letters of their names... does it need only enough to be a unique identifier? – mlegge Jun 11 '14 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.