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

Hi,

I need to retrieve the following line from UBB file and replace some data within with variables value. The line is:

grep -iw NASIT GPPconfig.ubb.orig.06Nov2007 OPENINFO="Oracle_XA:Oracle_XA+Acc=P/NASIT/nasit+SesTm=60+SqlNet=QA +NASIT+LogDir=/users/nas/disk1/data/traces+DB=QANASIT+Objects=true+Thr +eads=true"

The variables are: $DBUser, $DBPass, $DBSid Should to replace the data in the line as describe below:

OPENINFO="Oracle_XA:Oracle_XA+Acc=P/$DBUser/$DBPass+SesTm=60+SqlNet=$D +BSid+LogDir=/users/nas/disk1/data/traces+DB=$DBSid+Objects=true+Threa +ds=true"

And save the file with the new data.
some part of the line are contestant so we can use regular expression - Those parts are marked with Bold and underline:

OPENINFO="Oracle_XA:Oracle_XA+Acc=P/$DBUser/$DBPass+SesTm=60+SqlNet=$DBSid+LogDir=/users/nas/disk1/data/traces+DB=$DBSid+Objects=true+Threads=true"

Please advice the best way to do it.
Regards.
Noame.

20071111 Janitored by Corion: Removed HTML, added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Regular expression
by moritz (Cardinal) on Nov 11, 2007 at 08:30 UTC
    Have a look at split, perlretut and perlre. It seems promising to split at the + and handle the different chunks separately, but you have to know a bit more about the structure of the data.

    BTW you don't need to specify a doctype, header and body-tags here.

Re: Regular expression
by Krambambuli (Curate) on Nov 11, 2007 at 09:48 UTC
    It might be not the best way to do it, but for the regexp part I'd go with something like
    use strict; use warnings; my $line = 'OPENINFO="Oracle_XA:Oracle_XA+Acc=P/NASIT/nasit+SesTm=60+S +qlNet=QANASIT+LogDir=/users/nas/disk1/data/traces+DB=QANASIT+Objects= +true+Threads=true"'; my $DBUser = '$DBUser'; my $DBPass = '$DBPass'; my $DBSid = '$DBSid'; my $openinfo_rx = qr/ \A OPENINFO=" # openinfo line start ( [^"]+ ) # whatever inside the '"'s " \z # openinfo line end /x; if ($line =~ m/$openinfo_rx/) { my $openinfo_content = $1; my $quoted_openinfo_content = quotemeta( $1 ); my (@openinfo_parts) = split( /$quoted_openinfo_content/, $line ); $openinfo_content =~ s{ =P/ # =P/ starts user/pass part ([^+]+) # whatever except + \+ # end mark for user/pass part } {=P/$DBUser/$DBPass+}x; $openinfo_content =~ s{ \+(SqlNet|DB)= # ([^+]+) # whatever except + \+ # end mark } {+$1=$DBSid+}xg; $line = join( $openinfo_content, @openinfo_parts); } print $line;
    That assumes however that the strings used to anchor the parts that will be replaced won't never be also part of the text to be replaced - in your example, you migt have for example '+SqlNet=XXX+' instead of 'nasit' as password.
    A supposition that might be true or not, so maybe the code would need to be tightened a bit if there are no known restrictions on the input data.

    Hope that helps.

    Krambambuli
    ---
    enjoying Mark Jason Dominus' Higher-Order Perl
      in continuation to my previous question:

      I need to replace specific text in file.

      Foe example, I have 2 variables $var1, $var2 - and i want to found them in the file, present their values to user before the replacement and after:

      Before:
      dbuser = user1
      dbpass= pass1
      After:
      dbuser= user2
      dbpass=pass2

      With the following command i retrieve the old value from the file:

      @results = $telnet->cmd("perl -ne \"print if /\\b$oldDBUser/i ||/\\b$o +ldDBPass/i \" $ubbFile"); print "\n\n@results \n";

      How can I changed them value in @results in order to present the new list to the user. Then I want to get the user confirmation and perform the replacement in the file. Please be aware that all the command should be from telnet. If you have other suggestion to perform that process it'll be fine with me.

      Please advice.

      Edit: g0n - removed excess tags

        I'm sorry, but I'm not really following. Maybe you can clarify a bit what's happening 'there' (at the telnetted end) and what's happening 'here' - you seem to have Perl code on both sides and I'm rather confused about what's what.

        I'd assume that the remote end would hand over a text line and than get it back, maybe changed - but I'm not at all sure that I got it right.

        Maybe you can first solve the problem locally - working directly on a file, maybe using some helper subroutines like 'get_a_line' and 'put_a_line' - and then change the program to give get_a_line and put_a_line telnetting power.

        If telnet is necessary at all; I suppose that depends very much on what your powers are 'here' and what your powers are 'there', but overall the whole thing looks a bit weird.

        Krambambuli
        ---
        enjoying Mark Jason Dominus' Higher-Order Perl