Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

one liner?

by Anonymous Monk
on Aug 03, 2012 at 12:16 UTC ( #985222=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've a simple little script that pads leading '0' characters (max 5 characters) to the last field of a csv file.

I just wondered if this could be turned into a one-liner?

open FH,"< $ARGV[0]" or die $!; while (<FH>) { my ($pre, $last) = $_ =~ m/(.*),(\w+)/; $last = substr(("0" x 5) . $last, -1 * 5, 5); print "$pre,$last\n"; } close FH;

Comment on one liner?
Download Code
Re: one liner?
by BrowserUk (Pope) on Aug 03, 2012 at 12:24 UTC

    Yes :)

    C:\test>copy con in.csv 123,456 789,123 135,780 ^Z 1 file(s) copied. C:\test>perl -F, -aple"$_ = join ',', $F[0], sprintf '%05d',$F[1]" in. +csv 123,00456 789,00123 135,00780

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      I forgot about sprintf being able to do that. Thanks.

      Though that solution won't work for a file with more columns.

        perl -F, -aple"$_ = join ',', @F[0 .. $#F-1], sprintf '%05d',$F[-1]" in.csv

        This works even if each line has a different number of fields.

        CountZero

        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
        Though that solution won't work for a file with more columns.

        This will:

        perl -F, -aple"$F[-1]=sprintf'%05d',$F[-1]; $_=join',',@F" in.csv

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: one liner?
by jwkrahn (Monsignor) on Aug 03, 2012 at 15:15 UTC
    perl -lpe's/([^,]+)$/ sprintf "%05s", $1 /e' yourfile

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985222]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (16)
As of 2014-07-11 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (230 votes), past polls