Productive Linux


 Subscribe via Feedburner in a reader

Enter your email address:

Delivered by FeedBurner


Don't have an account?
Sign up to
Forgot your password?

Sort text files
3 February 2005 @ 12:45 GMT
by Paul

Recently, I had a text file that I had created by using pdf2text - it's a great way to get data out of pdfs into a useful format. Anyway, the data ended up being in "filename.txt" the form:

2 Peter
7 Alice
5 Fred

I needed it sorted. But how? The "sort" command came ot the rescue. Typing this:

sort filename.txt

Gave this output:

2 Peter
5 Fred
7 Alice

However, I wanted it sorted on the second field, that is the names. "sort" can do this too:

sort -d -k 2 filename.txt

Brief explanation: The '-d' switch stands for 'dictionary sort' and ensures that sorting takes place alphabetically as a dictionary would do it. The '-k' switch stands for 'key' and with the '2' tells sort to sort on the second field in the file, that is the names. The fields themselves are separated by spaces.

how would you sort on the second field in the case that the text file was like this: 2Peter 7Alice 5Fred

Hohw would you tell the sort command to sort on second field?


Posted by Arturo on 2008-11-05 20:28:00.
Comments disabled