LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to sort this .txt file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-sort-this-txt-file-797094/)

cliffyao 03-22-2010 01:39 PM

how to sort this .txt file
 
Hi, I have this .txt file

3183630 R
3574216 W
5264994 M
2656889 A

I want to sort it by the numerical order of column. After sort, I wish to get output like

2656889 A
3183630 R
3574216 W
5264994 M

anyone knows how to use sort to do that? or any other command?

thanks a lot

Tinkster 03-22-2010 01:53 PM

sort -n


man sort
for details


Cheers,
Tink

cliffyao 03-22-2010 03:46 PM

Hi, Tink

Thanks for your response. I used

sort +0 -1 input.txt > output.txt

but it can't sort the first column by numerical order... It seems treating the 1st column as characters. For example, after running my command, I got this

1001084 R
10011 R
10012 S
1003918 R
1004172 R
1005844 S
100707 S
1011598 R
1013262 R
1020374 W

Do you know the right option I should use with sort?

Thanks

w1k0 03-22-2010 03:51 PM

cliffyao, did you indeed read the entire Tinkster's post?

colucix 03-22-2010 03:55 PM

Have you tried what Tinkster suggested? sort -n forces numerical order.

Edit: repetita iuvant.. ;)

cliffyao 03-22-2010 04:11 PM

oops, my bad..I thought "sort -n" is like sort the nth column, just like my "sort -1". Tink, sorry for misunderstanding your post.

I tried

sort -n input.txt > output.txt

and it is working.

But, how about if my numbers are not in the first column?

Thanks everbody.

w1k0 03-22-2010 04:18 PM

Quote:

Originally Posted by cliffyao (Post 3907997)
But, how about if my numbers are not in the first column?

Read the rest of Tinkster's post, run man sort and search for -k switch (--key).


All times are GMT -5. The time now is 08:18 PM.