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!
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!
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!?
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!?
@lines = <IN>;
print @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
print @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
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.
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.
ASKER
When I try to use the code above (in last comment) I get an error 500??
What can be the cause of that?
What can be the cause of that?
ASKER
Î fixed the error 500 myself. After
open(OUT there has te be a ,
But could you please try to answer my other question?
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);
$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);
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);