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?

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. What is the meaning of 1,1n here?
Oh ok. So, say that field 3 contains strings, I will do -k 3,3d to sort by dictionary order, right?
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.

1 Comment

Thanks. Does sort allow also to specify for each field whether I want to sort numerically or alphabetically? (see my last edit)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.