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!
mmcwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
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);
0
mmcwAuthor Commented:
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!?


0
ozoCommented:
@lines = <IN>;
print @lines[sort {$lines[$a] <=> $lines[$b] || $a <=> $b} 0..$#lines];
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

mmcwAuthor Commented:
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.
0
mmcwAuthor Commented:
When I try to use the code above (in last comment) I get an error 500??
What can be the cause of that?
0
mmcwAuthor Commented:
Î fixed the error 500 myself. After
open(OUT there has te be a ,

But could you please try to answer my other question?
0
ozoCommented:
$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);
0
mmcwAuthor Commented:
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?

0
ozoCommented:
$settings = "test.txt";
$settings_sort = "test.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);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.