Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Replace a string after a word

by nithins (Sexton)
on Jan 06, 2014 at 08:35 UTC ( #1069470=perlquestion: print w/ replies, xml ) Need Help??
nithins has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, How to replace string after matching with a certain word in a file?

For example, i have a string in file say "user={hhhfj}jshdfjhdfjh\=" i want to match for "user=" , if match is successful i want to replace this "{hhhfj}jshdfjhdfjh\=" string with my own value & new value should be written to file i,e "user=newvalue".

Comment on Replace a string after a word
Re: Replace a string after a word
by walto (Pilgrim) on Jan 06, 2014 at 09:17 UTC
    As I understood you want the filename to be user=newvalue and the string "user=newvalue" to be written to that file:
    #!/usr/bin/perl # # use strict; use warnings; my $teststring = '"user={hhhfj}jshdfjhdfjh\="'; my $own_value = "newvalue"; if ( $teststring =~ /\"user=.*/ ) { $teststring =~ s /\"user=.*/\"user=$own_value\"/; my $filename = $teststring; print $teststring, "\n"; $filename =~ s/\"//g; #you don't want " in an filename open my $outfile, ">", $filename; print $outfile $teststring; close $outfile; }
Re: Replace a string after a word
by Anonymous Monk on Jan 06, 2014 at 09:18 UTC

    What you could do is use the built-in string functions like index() and substr(). You could also use regular expressions.

Re: Replace a string after a word
by kcott (Abbot) on Jan 06, 2014 at 15:04 UTC

    G'day nithins,

    A little more information regarding the file and the string to be replaced would have been useful. For instance, how big is the file? how many replacements are possible in the file? if more than one, how many replacements are possible per record? Also, are there any constraints such as time, memory or disk space?

    Bearing in mind the unknowns, the following (pm_1069470.pl) may be suitable for your needs:

    #!/usr/bin/env perl use strict; use warnings; use autodie; use Tie::File; tie my @records, 'Tie::File', 'pm_1069470.txt'; s/user={hhhfj}jshdfjhdfjh\\=/user=newvalue/ for @records; untie @records;

    Sample run:

    $ cat pm_1069470.txt blah blah blah user={hhhfj}jshdfjhdfjh\= whatever user=something else user={hhhfj}jshdfjhdfjh\= other data $ pm_1069470.pl $ cat pm_1069470.txt blah blah blah user=newvalue whatever user=something else user=newvalue other data

    -- Ken

Re: Replace a string after a word
by nithins (Sexton) on Jan 07, 2014 at 07:16 UTC

    Thank you guys!!. Your solutions helped me a lot...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-09-01 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls