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

changing name within a file

by DS (Acolyte)
on Aug 29, 2002 at 22:31 UTC ( [id://193938]=perlquestion: print w/replies, xml ) Need Help??

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

Hey folks , I am trying to grep for a line containing the following :
:dir=../somthing.cpp
and change it to the following :
:dir=../mainDir/sudir/somthing.cpp
I am trying :
while <out> { if ( m/^:dir/) $_ = s/$_/:dir=../mainDir/sudir/somthing.cpp/; }
I am having a littel bet hard time with the .cpp file.. can someone give me an idea .. thanks a lot

Replies are listed 'Best First'.
Re: changing name within a file
by DamnDirtyApe (Curate) on Aug 29, 2002 at 22:50 UTC

    Well, as for the search-and-replace, this should do it:

    while ( <DATA> ) { s{^:dir=.*?([^/]*)$}{:dir=../mainDir/sudir/$1} ; print ; }

    Also, since it sounds like your trying to replace all instances within a file, you might be able to save yourself a little bit of work with Tie::File.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: changing name within a file
by greenFox (Vicar) on Aug 30, 2002 at 00:38 UTC
    If I understood correctly-

    perl -pi.bak -e 's# :dir=\.\./(somthing.cpp)# :dir=\.\./mainDir/sudir/$1#' file

    If you want to do this as part of a larger script take a look at Tie::File or Editing Files In Place

    Update:added :dir=

    Update:eeek! I just noticed that this node is awfully like this one have I failed dws's homework appeal?

    --
    Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-23 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found