Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: One liner to subtract by 2

by choroba (Canon)
on Jul 24, 2013 at 14:05 UTC ( #1046122=note: print w/ replies, xml ) Need Help??


in reply to One liner to subtract by 2

You can use the /e switch of the substitution operator:

perl -pe 's/([0-9]{10})$/sprintf "%010d",$1-2/e'

Update: I am not sure how to search for the line, though, because you did not give any information on how these lines are structured. Adding that to the one-liner is easy, e.g. let's pretend we want to process lines beginning with 0TONY:

perl -pe 's/([0-9]{10})$/sprintf "%010d",$1-2/e if /^0TONY/'

Update 2: sprintf added. Thanks derby.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: One liner to subtract by 2
Select or Download Code
Replies are listed 'Best First'.
Re^2: One liner to subtract by 2
by derby (Abbot) on Jul 24, 2013 at 14:08 UTC

    You'll lose leading zeros with that.

    -derby

    update: so perl -pe 's/([0-9]{10})$/sprintf("%010d", $1-2)/e'

      Thanks, fixed.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Thanks Guys

        the header is on the first line of my file so I'm doing the following

        perl -pe 's/(0-9{10})$/sprintf("%010d", $1-2)/e if $. ==1' TONYFILE.DAT

        for some reason it's not changing the count...it's still 496

        any idea what i'm missing here?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2015-07-08 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls