Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Also, if your on Windows like me, I open($file, '<', shift) or die "$!"; and then immediately binmode($file);.

Commands or parameters or filenames added to the command line when calling the script get put into an array called @ARGV and when you call shift it increments $ARGV[0] to $ARGV[1] to $ARGV[2] and so on for each shift used.

So, if you used C:\path\to\script\perl my_script.pl file_1.txt outfile.txt then you could use shift again to open() (use three arg open) and instead of printing it to the console window, you can write the output to $outFile.

use strict; use warnings; my %hash; open (my $inFile, '<', shift) or die "$!"; open (my $outFile, '>', shift) or die "$!"; binmode($inFile); binmode($outFile); while (<$inFile> =~ /(\d)\s+(\d)\s+(\d)\s+(\w+)/){ push @{$hash{$4}}, $1, $2, $3; } print $outFile "@{$hash{$_}}[0..2] $_\n" for sort keys %hash;
Usage: C:\path\to\script\perl my_script.pl inFile.txt outFile.txt

Also, please note this also removes one space from each column per row. As long as that does not corrupt your data set it should be fine. It actually may save you some hard drive space. :)

EDITED: fixed typo in matching patterns, thanks haukex

EDITED: changed and made obvious that the individual needs to make absolutely certain that this does not corrupt anything in their data set.

EDITED: had to add a new paragraph so my second EDIT looked ok.


In reply to Re^5: Sort alphabetically from file by james28909
in thread Sort alphabetically from file by edujs7

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-25 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found