Delete columns from space delimited file where file header matches

后端 未结 5 1533
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-25 13:58

I have a space delimited input text file. I would like to delete columns where the column header is size using sed or awk.

Input File:

id quantity colour         


        
5条回答
  •  既然无缘
    2021-01-25 14:29

    If you have GNU cut available this can be done like so:

    columns=$(head -n1 INPUT_FILE \
              | tr ' ' '\n'       \
              | cat -n            \
              | grep size         \
              | tr -s ' '         \
              | cut -f1           \
              | tr -d ' '         \
              | paste -sd ",")
    
    cut --complement -d' ' -f$columns INPUT_FILE
    

    Which generates a comma separated list based on the heading, and then cuts the complement of that list from INPUT_FILE.

提交回复
热议问题