LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with sorting a file (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-sorting-a-file-605439/)

Yogiz 12-08-2007 04:04 PM

Help with sorting a file
 
Hi there,

I would like to ask how I could sort this nam input file;

Code:

+ 2.47 4 3 cbr 500 ------- 3 4.0 1.1 294 989
+ 2.47 1 2 cbr 500 ------- 2 1.0 5.0 394 990
+ 2.47 2 3 cbr 500 ------- 2 1.0 5.0 313 828
+ 2.471 3 2 cbr 500 ------- 3 4.0 1.1 292 985
+ 2.472 3 5 cbr 500 ------- 1 0.0 5.0 233 600

so that the output file is in ascending order by last column "989", "990" etc

Code:

+ 2.472 3 5 cbr 500 ------- 1 0.0 5.0 233 600
+ 2.47 2 3 cbr 500 ------- 2 1.0 5.0 313 828
+ 2.471 3 2 cbr 500 ------- 3 4.0 1.1 292 985
+ 2.47 4 3 cbr 500 ------- 3 4.0 1.1 294 989
+ 2.47 1 2 cbr 500 ------- 2 1.0 5.0 394 990

Thanks in advance for your input.

Yogiz

Samotnik 12-08-2007 04:17 PM

Use sort(1) utility

colucix 12-08-2007 04:17 PM

If the number of columns is fixed, e.g. each rows have always 12 fields, you can use sort with option -k to specify the position of the field to be sorted:
Code:

sort -n -k 12 input_file

Yogiz 12-08-2007 04:31 PM

Awsome thanks for that.

How would I go on about then extracting just the first column into a new file?

Yogiz 12-08-2007 04:33 PM

Awsome thanks for that.

How would I go on about then extracting just the second column into a new file?
For example;

2.472
2.47
2.471
2.47
2.47

Yogiz 12-08-2007 04:41 PM

Got it used

awk '{print $2}' filename > output

Love Linux


All times are GMT -5. The time now is 05:21 AM.