Subject: bug#13301: patch to preserve field order in cut

From: Bob Proulx <[email-masked]>
Date: Fri, 28 Dec 2012 17:06:48 -0700

severity 13301 wishlist
thanks

Brad Cater wrote:
> I found that
> echo "a,b,c" | cut -d"," -f1,2
> gives the same result as
> echo "a,b,c" | cut -d"," -f2,1

This is because 'cut' has always behaved that way way back forty years
for forever.  So people like me don't consider it a bug.  It is just
the way it was written to work.

The GNU manual documents it this way:

  The list elements can be repeated, can overlap, and can be specified
  in any order; but the selected input is written in the same order
  that it is read, and is written exactly once.

> This means that it's necessary to use another process to re-order columns.

The standard solution is to use 'awk'.  It also has a lot of years
behind it.

  $ echo "a,b,c" | awk -F, '{print$2,$1}'
  b a

Using awk also allows duplication of fields.

  $ echo "a,b,c" | awk -F, '{print$2,$1,$2}'
  b a b

> I have written a patch for cut.c ...

I have not looked at the patch but the barrier to adding new short
options is pretty high.  I will leave that for others to comment.
Personally I don't think it is necessary since awk is already standard
and therefore use of the feature is already available everywhere that
you need it without any changes.

This issue is discussed periodically.  Here are a few that I found
with a quick search.

  http://lists.gnu.org/archive/html/bug-coreutils/2005-06/msg00125.html
  http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00020.html
  http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00169.html

Bob