Forcing the order of output fields from cut command

后端 未结 2 779
耶瑟儿~
耶瑟儿~ 2021-02-01 14:40

I want to do something like this:

cat abcd.txt | cut -f 2,1 

and I want the order to be 2 and then 1 in the output. On the machine I am testing

2条回答
  •  深忆病人
    2021-02-01 15:09

    Lars' answer was great but I found an even better one. The issue with his is it matches \t\t as no columns. To fix this use the following:

    awk -v OFS="  " -F"\t" '{print $2, $1}' abcd.txt
    

    Where:

    -F"\t" is what to cut on exactly (tabs).

    -v OFS=" " is what to seperate with (two spaces)

    Example:

    echo 'A\tB\t\tD' | awk -v OFS="    " -F"\t" '{print $2, $4, $1, $3}'
    

    This outputs:

    B    D    A    
    

提交回复
热议问题