Link to home
Start Free TrialLog in
Avatar of mmcw
mmcw

asked on

Sort a file

How to sort the file called test.txt.
The input of the file looks like:

0|ba|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3
4|ba|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3

I want to sort on the number in the first row.
The number 0,2,2,4,5,5

How to sort them!!

0|ba|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3
4|ba|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3

has to be sorted!
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Do you mean sort first column in ascending order? You could call the Unix command line sort from perl:
system("sort test.txt > sorted.txt");

Alternatively:
open(IN, "<test.txt");
@lines = <IN>;
open(OUT ">sorted.txt");
print(OUT sort @lines);
close(IN);
close(OUT);
Avatar of mmcw
mmcw

ASKER

Hello,

Thank you for your quick answer.
I have still a little problem (I hope)

When I use the system sort command, the sorting will go to the second line if the first row values are the same. When the first row values are the same I want the order be original.

When I sort the coden down here,

2|ca|sdsjhdg|dkhsfjkh|3
5|va|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3
4|ga|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3

I want it to sort to:

2|ca|sdsjhdg|dkhsfjkh|3
2|ba|sdsjhdg|dkhsfjkh|3
4|ga|sdsjhdg|dkhsfjkh|3
5|va|sdsjhdg|dkhsfjkh|3
5|ba|sdsjhdg|dkhsfjkh|3

is this possible!?


Avatar of ozo
@lines = <IN>;
print @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
Avatar of mmcw

ASKER

Question to OZO:

Where to place these lines.
Do I have to replace the lines in terraplanes answer!

$settings = "test.txt";
$settings_sort = "sorted.txt";

open(IN, "<$settings");
@lines = <IN>;
open(OUT ">$settings_sort");
print @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
close(IN);
close(OUT);

Is it also possible to write the sorted values to the same file (in example test.txt) and not to a other file (in example sorted.txt). I want the test.txt be sorted.
Avatar of mmcw

ASKER

When I try to use the code above (in last comment) I get an error 500??
What can be the cause of that?
Avatar of mmcw

ASKER

Î fixed the error 500 myself. After
open(OUT there has te be a ,

But could you please try to answer my other question?
$settings = "test.txt";
$settings_sort = "sorted.txt";

open(IN, "<$settings");
@lines = <IN>;
close(IN);
open(OUT, ">$settings_sort");
print OUT @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
close(OUT);
Avatar of mmcw

ASKER

Thank you for your answer!

But what about the other question I asked.

Is it also possible to write the sorted values to the same file (in example test.txt) and not to a other file (in example sorted.txt). I want the test.txt be sorted.

I do not want to use: the file sorted.txt or is the only solution to rename the sort.txt to test.txt?

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial