13

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?

Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185

2 Answers2

22

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
Vijay
  • 65,327
  • 90
  • 227
  • 319
9

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Thanks. Does `sort` allow also to specify for each field whether I want to sort numerically or alphabetically? (see my last edit) – Ricky Robinson Aug 13 '12 at 13:20