coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'sort' command -- support more than one field-delimiter


From: Pádraig Brady
Subject: Re: 'sort' command -- support more than one field-delimiter
Date: Sun, 15 Oct 2023 23:01:14 +0100
User-agent: Mozilla Thunderbird

On 15/10/2023 14:28, Dani Moncayo wrote:
Consider a file like [1].
(Note that, in each line, the dates are in (month/day/year) format,
_without_ zero-padding)

I want to sort that file by date.

After looking at sort's documentation (and also searching a bit on the
internet), I fail to see a way to achieve that sorting, by just using
the "sort" command.

It seems that one can specify just _one_ character as field separator.
I was expecting to be able to specify more than one. Something like:
$ sort -n -t' /' -k5 -k3 -k4 file1.txt

I think it would give the flexibility I was looking for here.

What do you think?

Yes it's a fair point, though a bit of an edge case,
i.e. a fixed number of fields separated by different delimiters.

The more general approach is to use the DSU pattern
(which is mentioned in the info manual), to adjust the data.
For your case you could do something like:

  sed 's/\(.*\) \([0-9/]\+\)$/\2 \1 \2/' < file1.txt |
  sort -t/ -n -k3,3 -k1,1 -k2,2 |
  cut -d' ' -f2-

Also if we were to implement this, then it might introduce confusion.
For example users might try to `sort -t "blah"` expecting
the word "blah" to be used as a delimiter.
This is similar to the python "".rstrip() string method,
which takes chars, but I've seen many many instances
of code doing things like filename.rstrip(".txt")
which is an insidious error as it often works.

cheers,
Pádraig



reply via email to

[Prev in Thread] Current Thread [Next in Thread]