Extract specific columns from delimited file using Awk

后端 未结 8 1907
-上瘾入骨i
-上瘾入骨i 2020-11-28 05:44

Sorry if this is too basic. I have a csv file where the columns have a header row (v1, v2, etc.). I understand that to extract columns 1 and 2, I have to do: awk -F \

8条回答
  •  孤独总比滥情好
    2020-11-28 06:16

    Others have answered your earlier question. For this:

    As an addendum, is there any way to extract directly with the header names rather than with column numbers?

    I haven't tried it, but you could store each header's index in a hash and then use that hash to get its index later on.

    for(i=0;i<$NF;i++){
        hash[$i] = i;
    }
    

    Then later on, use it:

    j = hash["header1"];
    print $j;
    

提交回复
热议问题