Results 1 to 5 of 5
If I have a txt file that looks like this:
James_Bill 6
Lee_Kim 6
Cain_Tommy 6
James_Bill 8
Lee_Kim 8
Cain_Tommy 8
James_Bill 12
Lee_Kim 12
Cain_Tommy 12
James_Bill 20
...
- 05-05-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 2
Help with sort command
If I have a txt file that looks like this:
James_Bill 6
Lee_Kim 6
Cain_Tommy 6
James_Bill 8
Lee_Kim 8
Cain_Tommy 8
James_Bill 12
Lee_Kim 12
Cain_Tommy 12
James_Bill 20
Lee_Kim 20
Cain_Tommy 20
James_Bill 50
Lee_Kim 50
Cain_Tommy 50
James_Bill 100
Lee_Kim 100
James_Bill 500
Lee_Kim 500
Cain_Tommy 500
James_Bill 1000
Lee_Kim 1000
Cain_Tommy 1000
But I need to sort it alphabetically by the first column and then numerically by the second column so that it looks like this:
Cain_Tommy 6
Cain_Tommy 8
Cain_Tommy 12
Cain_Tommy 20
Cain_Tommy 50
Cain_Tommy 100
Cain_Tommy 500
Cain_Tommy 1000
James_Bill 6
James_Bill 8
James_Bill 12
James_Bill 20
James_Bill 50
James_Bill 100
James_Bill 500
James_Bill 1000
Lee_Kim 6
Lee_Kim 8
Lee_Kim 12
Lee_Kim 20
Lee_Kim 50
Lee_Kim 100
Lee_Kim 500
Lee_Kim 1000
How can I use the sort command to accomplish this?
Thanks in advance!
- 05-06-2009 #2
Use sort command to sort the file
If you want the content of the file text to be sorted in another file( which you can rename afterward to text) use the sort command.
The above code will create a file tex1 with all content sorted.You can then delete the original file and rename the text1 to text. Hope this is what you were looking forCode:cat text | sort >> text1
- 05-06-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 2
So my problem is that when I sort the text file the numeric column looks like:
1000
100
500
12
20
50
6
8
for each of the names. I need them in order like:
6
8
12
20
50
100
500
1000
- 05-06-2009 #4
check out the manual for sort
Please check out the manual for sort command you will find something to sort data in reverse order( may be -r I'm not sure).
- 05-06-2009 #5
try this:
sort -k1d,1 -k2n,2 textfile
It says:
Sort the field 1 to 1 dictionary
and sort the field 2 to 2 numerical.


Reply With Quote