1

I've searched unsuccessfully for a solution to this problem.

I have a CSV file with a date field I'm trying to sort by both month and day.

CSV format : name,email,phone,date

The date field format is m/d/yy as in 1/3/84

I can sort by month

sort -t',' -n -k4 < $file`

but I can't wrap my head around splitting the date field and then sorting on k1 and k2, if that makes sense or is even remotely close.

This is clearly wrong `

cat $file | sort -t',' -n -k4 | sort -t'/' -n -k1 -k2

as the output is only sorted by day and k1 includes everything before the first /.

Looking to learn, not be spoon-fed.

EDIT:

Sample CSV

Name,Email,Phone,Birthday
MarkW.,email@email.com,(555) 444-4444,6/24/85
GeorgeT.,email@email.com,(555) 555-1413,4/2/82
BobA.,email@email.com,(555) 555-5050,11/15/85
TimP.,email@email.com,,4/11/18
JohnM.,email@email.com,(555) 555-1317,4/25/13
MikeT.,email@email.com,(555) 555-2114,4/10/91
BobR.,email@email.com,(555) 555-1383,7/13/14
TomW.,email@email.com,(555) 555-1980,12/25/08
JamesS.,email@email.com,(555) 555-2150,6/12/15
TimC.,email@email.com,(555) 555-8048,3/17/08
WallyG.,email@email.com,,7/21/00
KeithD.,email@email.com,,12/29/12
DaveM.,email@email.com,,1/10/48
DanMc.,email@email.com,(555) 555-6863,5/19/88
DickM.,email@email.com,(555) 555-9333,11/23/13
KenB.,email@email.com,(555) 555-6891,1/2/04
DaveMc.,email@email.com,,
MichaelL.,email@email.com,(555) 555-1575,9/15/13
RoryB.,email@email.com,(555) 555-555,1/25/17

Sample Output, best I've been able to come up with using

sort -t',' -n -k4 < $file

Expected output would be 1/10/84 appearing after 1/2/04 for example.

DaveMc.,email@email.com,,
Name,Email,Phone,Birthday
DaveM.,email@email.com,,1/10/48
KenB.,email@email.com,(555) 555-6891,1/2/04
RoryB.,email@email.com,(555) 555-555,1/25/17
TimC.,email@email.com,(555) 555-8048,3/17/08
GeorgeT.,email@email.com,(555) 555-1413,4/2/82
JohnM.,email@email.com,(555) 555-1317,4/25/13
MikeT.,email@email.com,(555) 555-2114,4/10/91
TimP.,email@email.com,,4/11/18
DanMc.,email@email.com,(555) 555-6863,5/19/88
JamesS.,email@email.com,(555) 555-2150,6/12/15
MarkW.,email@email.com,(555) 444-4444,6/24/85
BobR.,email@email.com,(555) 555-1383,7/13/14
WallyG.,email@email.com,,7/21/00
MichaelL.,email@email.com,(555) 555-1575,9/15/13
BobA.,email@email.com,(555) 555-5050,11/15/85
DickM.,email@email.com,(555) 555-9333,11/23/13
KeithD.,email@email.com,,12/29/12
TomW.,email@email.com,(555) 555-1980,12/25/08

It's sorted by month but I'm trying to also sort by day

1
  • 1
    Give an example of the text and the expected output. Mar 28, 2020 at 22:14

1 Answer 1

0

You could do something like this, with Miller - adding new month and day fields, sorting by them, then removing:

$ mlr --csvlite put '
    dmy = splitnv($Birthday,"/"); $m = dmy[1]; $d = dmy[2]
  ' then sort -n m,d then cut -x -f m,d file.csv
Name,Email,Phone,Birthday
KenB.,email@email.com,(555) 555-6891,1/2/04
DaveM.,email@email.com,,1/10/48
RoryB.,email@email.com,(555) 555-555,1/25/17
TimC.,email@email.com,(555) 555-8048,3/17/08
GeorgeT.,email@email.com,(555) 555-1413,4/2/82
MikeT.,email@email.com,(555) 555-2114,4/10/91
TimP.,email@email.com,,4/11/18
JohnM.,email@email.com,(555) 555-1317,4/25/13
DanMc.,email@email.com,(555) 555-6863,5/19/88
JamesS.,email@email.com,(555) 555-2150,6/12/15
MarkW.,email@email.com,(555) 444-4444,6/24/85
BobR.,email@email.com,(555) 555-1383,7/13/14
WallyG.,email@email.com,,7/21/00
MichaelL.,email@email.com,(555) 555-1575,9/15/13
BobA.,email@email.com,(555) 555-5050,11/15/85
DickM.,email@email.com,(555) 555-9333,11/23/13
TomW.,email@email.com,(555) 555-1980,12/25/08
KeithD.,email@email.com,,12/29/12
DaveMc.,email@email.com,,
2
  • Thank you! I wasn't aware Miller existed but it's exactly what I was looking for.
    – Ken DeWitt
    Mar 29, 2020 at 1:22
  • @KenDeWitt there may be a better way (in fact, there may be a better way even with Miller - I'm only just starting to learn it) Mar 29, 2020 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.