Bash: sort csv file by first 4 columns

2019-03-12 19:59发布

I have a csv file with fields delimited by ";". There are 8 fields, and I want to sort my data by the first 4 columns, in increasing order (first sort by column 1, then column 2, etc)

How I can do this from a command line in linux?

I tried with open office, but it only lets me select 3 columns.

EDIT: among the fields on which I want to sort my data, three fields contain strings with numerical values, one only strings. How can I specify this with the sort command?

2条回答
Ridiculous、
2楼-- · 2019-03-12 20:04

sort -k will allow you to define the sort key. From man sort:

-k, --key=POS1[,POS2]
       start a key at POS1 (origin 1), end it at POS2 (default end of line). 

So

$ sort -t\; -k1,4

should do it. Note that I've escaped the semi-colon, otherwise the shell will interpret it as an end-of-statement.

查看更多
贼婆χ
3楼-- · 2019-03-12 20:19

Try:

sort -t\; -k 1,1n -k 2,2n -k 3,3n -k 4,4n test.txt

eg:

1;2;100;4
1;2;3;4
10;1;2;3
9;1;2;3

> sort -t\; -k 1,1n -k 2,2n -k 3,3n -k 4,4n temp3
1;2;3;4
1;2;100;4
9;1;2;3
10;1;2;3
查看更多
登录 后发表回答