Extract specific columns from delimited file using Awk

后端 未结 8 1882
-上瘾入骨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:36

    Other languages have short cuts for ranges of field numbers, but not awk, you'll have to write your code as your fear ;-)

    awk -F, 'BEGIN {OFS=","} { print $1, $2, $3, $4 ..... $30, $33}' infile.csv > outfile.csv
    

    There is no direct function in awk to use field names as column specifiers.

    I hope this helps.

    0 讨论(0)
  • 2020-11-28 06:38

    You can use a for-loop to address a field with $i:

    ls -l | awk '{for(i=3 ; i<8 ; i++) {printf("%s\t", $i)} print ""}'
    
    0 讨论(0)
提交回复
热议问题