Delete columns from space delimited file where file header matches

后端 未结 5 1571
爱一瞬间的悲伤
爱一瞬间的悲伤 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:21

    awk command

    awk '
    NR==1{
        for(i=1;i<=NF;i++)
            if($i!="size")
                cols[i]
    }
    {
        for(i=1;i<=NF;i++)
            if(i in cols)
                printf "%s ",$i
        printf "\n"
    }' input > output
    

    pretty printing

    column -t -s ' ' output 
    

    result

    id  quantity  colour  shape     colour  shape      colour  shape
    1   10        blue    square    red     triangle   pink    circle
    2   12        yellow  pentagon  orange  rectangle  purple  oval
    

提交回复
热议问题