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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .