Friday 15 February 2013

Sort file using pipe in perl -


I'm looking for a way to sort the file using a pipe. I have examined various examples online but I am still confused.

Let me assume that I have a file called "perlRocks.txt" which I want to sort.

I have it so far:

  can open (sort, "| sort" and "do not die"); Off (sort);  

What am I missing?

You are typing pipe are writing. If you have a file already, you probably want to sort the content. See $ in with the example if you want to write something from your script, see the example below with $ out see the documentation for more forms. To sort the existing file, you must type in a new file and then rename it. It is best to use some shell for this task.

  Use strict; Use warnings; Use Autodi; Fixed use FILE_NAME_IN = & gt; 'PerlRocks.in'; Static FILE_NAME_OUT = & gt; 'PerlRocks.out'; Open my IN $, '-', 'sort', FILE_NAME_IN; While ( 

is a safe version for output pipes that avoids problems with shell interpretation of the FILE_NAME_OUT content (you can avoid this content but not ...)

< Pre> open your $ out, '| - 'or' close 'STDOUT; Open STDOUT, '& gt;', FILE_NAME_OUT; Exec 'sort' or die $ !; };

If you insist that you do not want to use the shell, you can use the pearl.

  Use strict; Use warnings; Use Autodi; Fixed use FILE_NAME_IN = & gt; 'PerlRocks.txt'; Static FILE_NAME_OUT = & gt; 'PerlRocks.txt'; # If you do not want to continue my $ pid = fork process with the Perl process, do not bother with fork; Die "can not fork" unless defined as $ pid; Until ($ pid) {# Close STDIN within this code in this case; Closed STDOUT; Open STDIN, '& lt;', FILE_NAME_IN; FILE_NAME_IN if FILE_NAME_OUT; Unlink FILE_NAME_IN; Open STDOUT, '& gt;', FILE_NAME_OUT; Exec 'sort' or die $ !; } Waiting speed ($ pid, 0);  

Note FILE_NAME_IN and FILE_NAME_OUT may be the same, but it is not sorted anyway in some versions of the file in the disc Even in time, one can be hidden and inaccessible. There is also a good module for such tasks.


No comments:

Post a Comment