Bash:按月、日对 CSV 进行排序

Bash:按月、日对 CSV 进行排序

我一直在寻找解决这个问题的方法,但没有成功。

我有一个带有日期字段的 CSV 文件,我试图按月份和日期排序。

CSV 格式: name,email,phone,date

日期字段格式m/d/yy 为 1/3/84

我可以按月份排序

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

但我无法专注于分割日期字段,然后对 k1 和 k2 进行排序,如果这有意义或者甚至非常接近的话。

这显然是错误的`

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

因为输出仅按天排序,并且 k1 包括第一个之前的所有内容/

想要学习,而不是填鸭式的。

编辑:

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

示例输出,我能想到的最好的使用

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

例如,预期输出将是 1/10/84,出现在 1/2/04 之后。

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

它按月排序,但我也尝试按天排序

答案1

你可以做这样的事情磨坊主- 添加新的月份和日期字段,按它们排序,然后删除:

$ 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],,

相关内容