Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Parse a file, replace a particular strings and save new file

by waytoperl (Beadle)
on Nov 13, 2013 at 04:15 UTC ( [id://1062317]=perlquestion: print w/replies, xml ) Need Help??

waytoperl has asked for the wisdom of the Perl Monks concerning the following question:

I'm new to perl and want to write my first perl code, that will be used in a project. Problem: I've a file, "FileName.lib". Need to replace "values" with 500 for string_name500(Transition_11) and with 100 for string_name100(Transition_11). After replace, I need to save updated file with NewFileName.lib Please help me. these file consists -->

string_name500(Transition_11) { values ( \ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 "); + string_name100(Transition_11) { values ( \ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ",\ "0, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008 ");

Replies are listed 'Best First'.
Re: Parse a file, replace a particular strings and save new file
by NetWallah (Canon) on Nov 13, 2013 at 05:16 UTC
    Welcome to the Monastery.

    The following code solves your problem as specified.

    If it does not do what you WANT, you need to improve your specification.

    perl -pe '$x++ ? s/\bvalues\b/100 for string_name100(Transition_11)/ : s/\bvalues\b/500 for string_name500(Transition_11)/ ' FileName.lib > NewFileName.lib
    Of course, it would help if you showed us what you TRIED, and explain what you have trouble with.

    Please read How do I post a question effectively?.

                 When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: Parse a file, replace a particular strings and save new file
by davido (Cardinal) on Nov 13, 2013 at 08:09 UTC

    How have you come to be a code contributor on a Perl project without first having a working knowledge of Perl? Rather than doing your project for you, we should be assisting you in gaining sufficient proficiency in Perl to accomplish this trivial task yourself.

    So where can we start? I suggest spending 20 to 30 minutes with perlintro. Then follow some of the links in that document to dive deeper into file IO, and regular expressions. An hour or two later you'll be competent to solve this, and many future problems yourself. And when you do come up with stumbling blocks in the future (as we all do), you'll be equipped with enough understanding of the Perl basics to be able to ask informed questions.


    Dave

Re: Parse a file, replace a particular strings and save new file
by ig (Vicar) on Nov 13, 2013 at 06:15 UTC
      Hi, Try this code
      my ($v1,$value,$i); open (FILE,"old.txt") or die $!; open (NEW,">new.txt") or die $!; while(<FILE>){ if($_ =~/string_/){ ($v1, $v2) = split(/name/,$_); ($value) = split(/\(/,$v2); } $_ =~s/values/$value/; print NEW $_; } ~

Log In?
Username:
Password:

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

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

    No recent polls found