26

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

1
  • Yes, you can import this type of data into Excel, if you import as text, then specify "|" as the delimiter. Commented Jun 10, 2011 at 13:50

5 Answers 5

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

Should do it. field separator and alternate key selection

Sign up to request clarification or add additional context in comments.

6 Comments

What is the purpose of the + in +2?
To move the sort from focusing on the first delimited group to the second delimited group.
@brandizzi: Habit. Older versions of sort used + and - to signal what columns to sort on and what to not sort on. GNU sort uses a different technique that doesn't require it (but also doesn't barf on +)
Means sort numerically on the second column. Although obsolete this notation works. The use of the -k 2,2n notation is recommended, at least on GNU sort.
@Alexander: The "n" mean numeric. In other words, assume that the second argument is a number so that 5 sorts before 10. When used in the context I used it, it applies the n argument only to argument 2. This is irrelevant since there is only one sort key, but for more complex commands it can be important.
|
13

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).

Comments

7

have you tried sort -n

$ sort -n inputFile
This is another sentence|5
This is a sentence|10
This is the last sentence|20

you could switch the columns with awk too

$ awk -F"|" '{print $2"|"$1}' inputFile
10|This is a sentence
5|This is another sentence
20|This is the last sentence

combining awk and sort:

$ awk -F"|" '{print $2"|"$1}' inputFile | sort -n
5|This is another sentence
10|This is a sentence
20|This is the last sentence

per comments

if you have numbers in the sentence

$ sort -n -t"|" -k2 inputFile
This is another sentence|5
This is a sentence|10
This is the last sentence|20
this is a sentence with a number in it 2|22

and of course you could redirect it to a new file:

$ awk -F"|" '{print $2"|"$1}' inputFile | sort -n > outFile

1 Comment

true, I was going off the example he had copied.
3

Try this sort command:

sort -n -t '|' -k2 file.txt

Comments

2

Sort by number, change the separator and grab the second group using sort.

sort -n -t'|' -k2 dataset.txt

Comments

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.