How to print and store specific named columns from csv file with new row numbers

前端 未结 3 895
挽巷
挽巷 2021-01-24 22:40

start by saying, I\'m very new to using bash and any sort of script writing in general.

I have a csv file that has basic column headers and values underneath which look

相关标签:
3条回答
  • 2021-01-24 23:07

    If you have a simple multi-space delimited file (as in your example) awk is the best tool for the job. To select the column by name in awk you can do something like:

    $ awk -v col="b" 'FNR==1 { for (i=1;i<=NF;i++) if ($i==col) x=i; next }
                      {print FNR-1 OFS $x}' file   
    1 3
    2 5
    3 5
    4 8
    
    0 讨论(0)
  • 2021-01-24 23:22

    Like this? Using awk:

    $ awk 'NR>1{print NR-1, $2}' file
    1 3
    2 5
    3 5
    4 8
    

    Explained:

    $ awk '              # using awk for the job
    NR>1 {               # for the records or rows after the first
        print NR-1, $2   # output record number minus one and the second field or column
    }' file              # state the file
    

    I would like to be able to do this for various different named column headers. With awk you don't specify the column header name but the column number, like you don't state b but $2.

    0 讨论(0)
  • 2021-01-24 23:28
    awk 'NR>1 {print i=1+i, $2}' file
    

    NR>1 skips the first line, in your case the header.

    print print following

    i=1+i prints i, i is first 0 and add 1, so i is 1, next time 2 and so on.

    $2 prints the second column.

    file is the path to your file.

    0 讨论(0)
提交回复
热议问题