Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

changing the first number of every line

by jinism (Initiate)
on Nov 21, 2012 at 12:55 UTC ( #1004912=perlquestion: print w/replies, xml ) Need Help??
jinism has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am relatively new to perl and would like to request for a code that would enable me to change the first number of a line of vectors for all lines in the txt file.

An example of the vectors looks like this:

0 1:120.000000 2:0.000000 3:120.000000 4:262.000000 5:120.000000 6:349.000000

Every line starts with 0, and i would like to replace that 0 with 1

I have around 100 of these lines in my file and any help would be greatly appreciated!

  • Comment on changing the first number of every line

Replies are listed 'Best First'.
Re: changing the first number of every line
by roboticus (Chancellor) on Nov 21, 2012 at 13:15 UTC


    You can get a good start with that by reading perlintro and look over perlfaq5, especially the section "How do I change, delete, or insert a line in a file, or append to the beginning of a file?".


    When your only tool is a hammer, all problems look like your thumb.

Re: changing the first number of every line
by choroba (Bishop) on Nov 21, 2012 at 13:14 UTC
    perl -i~ -p -e 's/^0/1/' *.txt
    -i changes the files, backups are created with the ~ appended
    -p prints each line after applying the code.
    The code just replaces 0 at the beginning of a line with 1.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: changing the first number of every line
by ww (Archbishop) on Nov 21, 2012 at 14:58 UTC
        ...and you can get a better start here if you'll read the docs; particularly, On asking for help and How do I post a question effectively?.
    Despite Parson choroba's generous response, this is NOT code-a-matic. We try to help those like yourself -- "new to perl" -- by helping them learn; not by solving their problems. So next time you have a problem like this; try to organize your thoughts about how you'd do the job on paper, and then, using perldoc -f function and the fine Tutorials here, try to come up with code to solve your quandry.

    If it works, you've really learned something; if it doesn't, and you can't figure out "why," then by all means, come back with the issue, your code (and data; each wrapped in <c>...</c> tags), an explanation of how your code fails to satisfy your intent, and verbatim quotes of any error messages or warnings.

    P.S. use strict; use warnings; or a recent enough version of Perl that invoking the version (for example, use 5.014;) does so for you. Those pragmas alone will help you find many sneaky little mistakes (to say nothing of big ones.

    PPS: If the solution choroba provided doesn't work for you, check that the quoting used is that required by your system -- windoze won't like what you've been given.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004912]
Approved by marto
[choroba]: megalag :-(
[erix]: ok, I looked it up, should have been: "Haribo macht Kinder froh und Erwachsene ebenso"
[Discipulus]: also mixing pizza (fermented/levited ) with juices is not so good
[erix]: D.: yeah, I am in the low countries. To be precise, I am 0.5m above sea level
[erix]: going for a run in the dunes - unless the storm (well, Beaufort 7) gets fiercer
Discipulus had stew with tomato sauce, onions, carrots and potatoes + bottle of red wine and still warm bred.. slept as dynosaur

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-11-22 09:11 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.