我想按照以下方式对csv进行排序,我想要的是
这就是我的所作所为:
$ sort -t"," -k2 -nk3 /tmp/test.csv
55b64670abb9c0663e77de84,525e3bfad07b4377dc142a24:9999,0.081032
5510b33ec720d80086865312,525e3bfad07b4377dc142a24:9999,0.081033
55aca6a1d2e33dc888ddeb31,525e3bf7d07b4377d31429d2:2,0.081034
55aca6a1d2e33dc888ddeb31,525e3bf7d07b4377d31429d2:2,0.081034
5514548ec720d80086bfec46,525e3bfad07b4377dc142a24:9999,0.081035
551d4e21c720d80086084f45,525e3bfad07b4377dc142a24:9999,0.081036
557bff5276bd54a8df83268a,525e3bfad07b4377dc142a24:9999,0.081036
这个结果很奇怪,它先按第3列排序,然后按第2列排序
答案 0 :(得分:3)
此命令似乎产生正确的输出:
sort -t"," -k2,2 -k3,3n /tmp/test.csv
我使用逗号将顺序限制为该列,并使用数字(-n
)开关来填充第三列中的最后一个字符。
它产生:
55aca6a1d2e33dc888ddeb31,525e3bf7d07b4377d31429d2:2,0.081034
55aca6a1d2e33dc888ddeb31,525e3bf7d07b4377d31429d2:2,0.081034
55b64670abb9c0663e77de84,525e3bfad07b4377dc142a24:9999,0.081032
5510b33ec720d80086865312,525e3bfad07b4377dc142a24:9999,0.081033
5514548ec720d80086bfec46,525e3bfad07b4377dc142a24:9999,0.081035
551d4e21c720d80086084f45,525e3bfad07b4377dc142a24:9999,0.081036
557bff5276bd54a8df83268a,525e3bfad07b4377dc142a24:9999,0.081036
答案 1 :(得分:0)
排序将对csv和txt文件中的数据进行排序,它将在控制台上打印输出
-t表示列以'|'分隔,-k1 -k2表示-它将按第1列然后按第2列对数据进行排序
$ sort -t '|' -k1 -k2 <INPUT_FILE>
要将结果存储在输出文件中,请使用以下命令
$ sort -t '|' -k1 -k2 <INPUT_FILE> -o <OUTPUTFILE>
如果要忽略标题行来执行此操作,请使用以下命令
(head -n1 INPUT_FILE && sort <(tail -n+2 INPUT_FILE)) > OUTPUT_FILE
head -n1 INPUT_FILE,它将仅打印文件的第一行,即标题
& 这种特殊的 tail 语法使您的文件从第二行到EOF。