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

hi monks,
U copy chandu.txt and guidpair.txt and try Chandu.txt is one file ->(below one)->file1 <here is my code>

5C1DAEF0-9386-44a5-B92A-31FE2C79236A 746B97B8-486A-094F-9426292F0C0BB644 7b8f231e-7176-45cc-94d7-083813b25da7 F1DD19D9-FE2A-7949-905A194FF091E251 7A46C182-8120-B94E-805DFAF46182136C 5F146C81-330A-854F-891B0D577D8801E2

I have to replace

Guidpair.txt is one file -> second file -> "Web","rad","INPUT","onclick","746B97B8-486A-094F-9426292F0C0BB644 F",""

Here I have to replace -> 746B97B8-486A-094F-9426292F0C0BB644 F from guidpair.txt to 5C1DAEF0-9386-44a5-B92A-31FE2C79236A (chandu.txt)

After that I have to store back to same place

Guidpair.txt file this one (below)-> file2
"Special","URL","","","http://10.0.1.49/ui/view_cx_login.php","" "Web","username","INPUT","onclick","","" "Web","username","INPUT","onfocusout","admin","" "Web","password","INPUT","onclick","","" "Web","password","INPUT","onfocusout","password","" "Web","submit_login","INPUT","onclick","Login","" "Special","URL","","","http://10.0.1.49/ui/do_local_replication.php"," +" "Web","onclick","B","onclick"," IMID-VKISHORE","" "Web","rad","INPUT","onclick","746B97B8-486A-094F-9426292F0C0BB644 F", +"" "Web","rad","INPUT","onfocusout","746B97B8-486A-094F-9426292F0C0BB644 +F","" "Web","Submit","INPUT","onclick","Start Replication","" "Web","onclick","B","onclick"," IMITS018","" "Web","dest","INPUT","onclick","F1DD19D9-FE2A-7949-905A194FF091E251 Z" +,"" "Web","dest","INPUT","onfocusout","F1DD19D9-FE2A-7949-905A194FF091E251 + Z","" "Web","ftps_source_secure_channel","INPUT","onclick","0","" "Web","ftps_source_secure_channel","INPUT","onfocusout","0","" "Web","ftps_dest_secure_channel","INPUT","onclick","0","" "Web","ftps_dest_secure_channel","INPUT","onfocusout","0","" "Web","fast_resync","INPUT","onclick","0","" "Web","fast_resync","INPUT","onfocusout","0","" "Web","submit","INPUT","onclick","Submit","" "Web","Logout","A","onclick","ui/view_cx_login.php",""

Task I done is this one

open (FILE1,'< chandu.txt'); open (FILE2,'+< guidpair.txt') or die("Can't open guidpair: $!"); while($b=<FILE1>) { print $b; $file=$b; while($a=<FILE2>) { if($a=~m/onclick/ && $a=~m/\w+-\w+-\w+-\w+\s\D/) { print $a; seek(FILE2,-127, 1); $a=~s/\w+-\w+-\w+-\w+\s\D/$file/; print FILE2 "$a \n"; } } } close(FILE1); close(FILE2);

20060717 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: how to match the pattern by using regexp for the below task
by shmem (Chancellor) on Jul 17, 2006 at 08:19 UTC
    # From what Anonymous monk writes, it seems the task is: # replace a field (in double quotes) of a comma # separated record in a file called guidpair.txt with # something else. # He gives me the line, the text of the last field, and # the replacement text which he somehow gets from another # file. # The line # "Web","rad","INPUT","onclick","746B97B8-486A-094F-9426292F0C0BB644 F +","" # should be # "Web","rad","INPUT","onclick","5C1DAEF0-9386-44a5-B92A-31FE2C79236A" +,"" # # Given that information, the program could look like this: my $infile = 'guidpair.txt'; my $tempfile = "$infile.new"; # =================================================== # CAVEAT: if $infile.new exists, it will be clobbered # XXX Better use File::Temp here? # =================================================== open(I,"<$infile") or die "Can't open $infile: $!\n"; open(O,">tempfile") or die "Can't write $tempfile: $!\n"; while(<I>) { if (/"Web","rad","INPUT","onclick","746B97B8-486A-094F-9426292F0C0 +BB644 F",""/) { s/746B97B8-486A-094F-9426292F0C0BB644 F/5C1DAEF0-9386-44a5-B92 +A-31FE2C79236A/; } print O; } close(I) or die "Can't close input file $infile: $!\n"; close(O) or die "Can't close temp file $tempfile: $!\n"; rename($tempfile,$infile) or die "Can't rename $tempfile to $infile: $!\n"; __END__
    This might be of no help at all, but your specs aren't clear. One way to make them clear is to write down your requirements into the program file as comments as accurate as possible. The clearer they are, the easier it is to translate them into code.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: how to match the pattern by using regexp for the below task
by cdarke (Prior) on Jul 17, 2006 at 07:51 UTC
    The file you are opening for read/write (+<) appears to be a text file with variable length records. You can't easily replace text records in place unless they are fixed length. There is really no such thing as a text file with records, it is just a string of characters, some of which happen to be new-line characters (that applies to UNIX and Windows anyway, some older systems did have real records).
    I suggest you change your approach and write out a new file, then rename it on completion.
    Please, don't use $a and $b, they are special variables in Perl and cause confusion.
    Remember to place ^ and $ around your RE when describing a whole line.
    Update: I misunderstood, and realise you are replacing strings of the same length. Your seek should probably be:
    seek(FILE2, -(length $a), 1); (and you should chomp $file). But it is still messy.
    Q. Are your data files realy double-spaced (\n\n between each record) or is that an aberation of cut-and-paste?
Re: how to match the pattern by using regexp for the below task
by rsiedl (Friar) on Jul 17, 2006 at 07:00 UTC
    I suggest using the preview button before posting...

    Update: Thanks Corion, much better
A reply falls below the community's threshold of quality. You may see it by logging in.