Cut Command Behaviour – Bug Or Feature

Home » CentOS » Cut Command Behaviour – Bug Or Feature
CentOS 12 Comments


I have a CSV file with three fields. eg.


I wanted the output to be:

the command cut -d, -f2,3,1



cut -d, -f2,3

works as advertised.

Is it specific to linux?

In that case how do I go about swapping two columns? I do not think a gazzillion byte gui is required.

The file size is about 43Megs.

any ideas?

12 thoughts on - Cut Command Behaviour – Bug Or Feature

  • Rajagopal Swaminathan wrote:

    I’ve never been that good with cut. I’m going to see the author, Dave Ihnat this weekend, who sometimes shows up here… but in the meantime, you might use awk ‘BEGIN {FS=”,”;}{print $2 “,” $3 “,” $1;}’ infile


  • Greetings,

    I tried it on an xp box with GnuWin32 binaries. It barked some error showing the single quote.

    I will try on a CentOS box later (which I am silently injecting
    [installing] into that env after handwaving the xp

  • cut cuts out, that what it does. I think it works exactly as advertised. On my system the manpage says

    …snip Selected input is written in the same order that it is
    read, and is written exactly once snap…


  • If you are sure that the fields will never contain commas, there are any number of ways to split lines into variables in any number of scripting languages including bash. However, if the input is
    ‘generic csv’ that is allowed to have quotes around fields with embedded commas it can be hard to parse. I’d recommend perl with the Text::CSV module so you don’t have to deal with parsing yourself.

  • Will not work. The “-f” flag specifies what columns; not the order. Everyone makes that mistake once :-)

    Use “awk”
    awk -F, ‘{print $2 “,” $3 “,” $1}’

  • See below

    I would think, yes.


    Scott’s Avatar
    Scott Scott is offline Forum Staff Administrator

    Join Date: Jun 2009
    Location: Switzerland – ZH
    Posts: 5,632
    Thanks: 136
    Thanked 596 Times in 515 Posts

    In AIX (for example) if you said

    echo 1,2,3,4,5 | cut -d, -f3,1,5

    you would get output as 3,1,5

    But the cut command in Linux behaves differently.

    Selected input is written in the same order that it is read, and is written exactly once.

    Which I think is poor. I can’t see any way to do what you want with cut.


    Thanks for everybody who pitched in

    Any the issue got transformed into something else altogether…. (PHB, vlookup ‘experts’ and the such for time being)

  • If you are willing to use tmp files you can:
    cut -d, -f1 tmp1
    cut -d, -f2 tmp2
    cut -d, -f3 tmp3

    paste -d, tmp1 tmp3 tmp2 >file_I_wanted rm tmp1 tmp2 tmp3

    There’s probably a clever way to do this with tee and fifos to handle unlimited stream sizes but I’m too lazy to work it out.

  • On a OS-X, and thats non arguably not linux-ish (means gnu-ish) but bsd-ish

    $ echo 1,2,3,4,5 | cut -d, -f3,1,5

    So it’s definitely not specific to linux.

    I tend to think that AIX is not posix compliant, then.

    The behaviour of cut makes sense to me, actually. I remember one of the UNIX paradigms and thats “do only one thing but do it good”

    However, I realize that I do not suggest a better way to do what you want to do, but I do not think I have to, there were so many hints in this thread pointing you to awk and I remember actually one shell only example.

  • Well, according to that document, AIX’s cut seems to behave as POSIX and Scott (on is simply wrong about the application’s behavior.

  • No need to guess about what AIX does:
    $ uname -svr
    AIX 1 6
    $ oslevel
    $ echo 1,2,3,4,5 | cut -d, -f3,1,5