Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting with unix tools and multiple columns

I am looking for the easiest way to solve this problem. I have a huge data set that i cannot load into excel of this type of format

This is a sentence|10 This is another sentence|5 This is the last sentence|20 

What I want to do is sort this from least to greatest based on the number.

cat MyDataSet.txt | tr "|" "\t" | ??? 

Not sure what the best way is to do this, I was thinking about using awk to switch the columns and the do a sort, but I was having trouble doing it.

Help me out please

like image 376
josephmisiti Avatar asked Jun 09 '11 15:06

josephmisiti


People also ask

How do I sort multiple columns in Unix?

Use the -k option to sort on a certain column. For example, use " -k 2 " to sort on the second column. In old versions of sort, the +1 option made the program sort on the second column of data ( +2 for the third, etc.).

Can sorting be done on multiple columns?

In Excel, you can sort your table by one or more columns, by ascending or descending order, or do a custom sort.

How do I sort multiple columns separately?

If you want to sort the table columns independently from each other, click on the Arrange All button in the ribbon toolbar tab Variables. After clicking, the Arrange_All function appears in the sidebar. If you click on it, one property will show in the Properties Panel - Desc.


2 Answers

sort -t\| -k +2n dataset.txt 

Should do it. field separator and alternate key selection

like image 152
Seth Robertson Avatar answered Oct 08 '22 00:10

Seth Robertson


You usually don't need cat to send the file to a filter. That said, you can use the sort filter.

sort -t "|" -k 2 -n MyDataSet.txt 

This sorts the MyDataSet.txt file using the | character as field separator and sorting numerically according to the second field (the number).

like image 38
Javier C Avatar answered Oct 08 '22 00:10

Javier C