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

From: Brad Cater <[email-masked]>
Date: Sat, 29 Dec 2012 11:20:04 -0500

Hi Bob

Thanks for the quick response.

I'm with you: let's not break something that has been working for a long
time. That's why I think that the new functionality should be a new option
rather than a replacement of existing behavior.

I read those example requests that you sent as well as a few on
Stackoverflow like this one:

http://stackoverflow.com/questions/1037171/forcing-the-order-of-output-fields-from-cut-command

It seems like people want this, but I couldn't find anyone who had written
it, so I thought that the barrier was the effort to make it work.
Admittedly, the patch that I submitted would require additional effort
since this is my first foray into Coreutils hacking. Even if it's not
adopted into the mainline, I'd be glad to read evaluations of the code so
that I could improve it for myself.

Thanks in general for GNU. It rocks.

-Brad

On Fri, Dec 28, 2012 at 7:06 PM, Bob Proulx <[email-masked]> wrote:

> 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
>