Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

perl sort on windows

by jarwulf (Initiate)
on Mar 25, 2013 at 00:52 UTC ( #1025209=perlquestion: print w/replies, xml ) Need Help??
jarwulf has asked for the wisdom of the Perl Monks concerning the following question:

system ("sort -k 1,1 -k 4n -T $workingDir $name >tempLoc_$name")==0 or die "$0 failed to sort $name"; So this piece of code fails on windows because I think the -k is interpreted as specifying the input twice. How would I change this so that its as equivalent as possible but runs on windows? Thanks

Replies are listed 'Best First'.
Re: perl sort on windows
by CountZero (Bishop) on Mar 25, 2013 at 06:58 UTC
    The sort that comes with my Windows (XP Pro) does not have a k option.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      And Windows utilities never have -something options, only /something options.

      jarwulf, can you do a "which sort" on the command prompt? If this works you must be in a *ix like environment under Windows (e.g. cygwin), which will lead to a very different answer than under "Windows rare".

      If not, you might want to consider getting one to avoid portability issues (well, many of them) in the future.

Re: perl sort on windows (sort.exe , gnusort.exe )
by Anonymous Monk on Mar 25, 2013 at 01:12 UTC

    cause I think the -k is interpreted as specifying the input twice. ow would I change this so that its as equivalent as possible but runs on windows? Thanks

    And what does the documentation say?

    I'm on windows, I have two sort.exe files on my system, one came with perl, the other is gnu sort

    They have different syntaxs

    I don't use the windows version much, but to avoid breaking some batch files and stuff that rely on it, I also named the gnu version gsort.exe

    You can distinguish between the two

    $ sort /? |head SORT [/R] [/+n] [/M kilobytes] [/L locale] [/REC recordbytes] [[drive1:][path1]filename1] [/T [drive2:][path2]] [/O [drive3:][path3]filename3] /+n Specifies the character number, n, to begin each comparison. /+3 indicates th +at each comparison should begin at the 3rd character in each line. Lines with fewe +r than n characters collate before other l +ines. By default comparisons start at the firs +t character in each line. $ gsort /? gsort: open failed: /?: Invalid argument

      one came with perl,

      one came with WINDOWS, jeez

      sort /+2 /+4 /T $windowsDir $name /O tempLoc_$name
      This should work I believe, only problem is with numeric sort.
Re: perl sort on windows
by topher (Scribe) on Mar 25, 2013 at 23:28 UTC

    This piece of code fails (note: it isn't Perl that's failing, it's your environment) because you are calling out to external tools and expecting them to behave similarly across multiple platforms. This is almost always going to bite you in the ass and cause problems.

    If you want it to be portable and run correctly on Windows, get rid of the external sort command, and do the work in Perl. Read in your file, write a little Perl sort routine, and you'll have something that will work everywhere you run it.

    Christopher Cashell
Re: perl sort on windows
by sundialsvc4 (Abbot) on Mar 27, 2013 at 13:33 UTC

    There are a number of external-sorting packages on CPAN.   Does anyone have experience with cross-platform packages that might help to “hide” this problem from concern?   (It would replace the present technique of executing an external CLI command.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025209]
Approved by NetWallah
Front-paged by NetWallah
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-05-24 01:15 GMT
Find Nodes?
    Voting Booth?