Hi

I am not an expert in Perl, hence requesting your help in this one. I have a user defined input which I need to parse and based on occurence of a pattern , need to replace certain lines on some text files based on the input file. So, for better understanding, I have got some text files , like a_copy.txt , a_paste.txt, a_cut.txt,b_copy.txt , b_paste.txt, b_cut.txt,c_copy.txt , c_paste.txt, c_cut.txt,d_copy.txt , d_paste.txt, d_cut.txt . Basically, my user input file looks like this:

NAME|VALUE = a TASK|VALUE = copy CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = z TASK|VALUE = cut CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = c TASK|VALUE = paste STACK|VALUE = 2 SHIP|VALUE = lowtier

Now , based on the input file, I first have to parse and if "NAME|VALUE = a" and the corresponding "TASK|VALUE = copy" , it should open a_copy.txt (if a_copy.txt doesn't exist, throw an error and exit, which is easy). Inside a_copy.txt, it has to check if the following lines with starting keywords like the input , i.e,

CAPS|VALUE = PKG_TYPE|VALUE =

exist and replace them in the a_copy.txt file with the input file values of "CAPS|VALUE = 0" and "PKG_TYPE|VALUE = premium". Any empty line in the input file should be ignored.

It should do this for all the text files corresponding to all the "NAME|VALUE = ??" and the corresponding "TASK|VALUE = ??" and replace all lines in the correct text file based on the input file values

.

I have managed to write a code which parses the input file for any format differences etc and report out, but I am stuck in writing the subroutine to do the replace the input file lines in the text files. My code is (although incomplete) is :

use Tk; use IO::Handle; #################### Check for correct usage of script################ +############ $mw = new MainWindow; my $num_args = $#ARGV +1 ; if ($num_args != 0) { $mw -> messageBox(-message=>"\nUsage: perl replay_file_changes.pl\n +"); exit; } ###################################################################### +############ ###################### User input #################################### +############################# print "\nPLEASE SPECIFY INPUT FILE\n"; $ip_file = <>; chomp $ip_file ; #check validity of user input if (-e $ip_file){ } else{ $mw -> messageBox(-message=> "\n$ip_file IS NOT FOUND\n"); exit; } ###################################################################### +############################ ###################### Real deal ##################################### +############################ open(INPUT_FILE, "<$ip_file") || die "\n!!!ERROR OPENING INPUT FILE. +EXITING SCRIPT!!!\n"; while(<INPUT_FILE>){ if ($_=~ /(.*) =\n/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /(.*) =\s+\n/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /(.*) = \s+(.*)/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /^NAME\|VALUE = (.*)/ ){ &check(); } } sub check { #print "\n$cell_name\n"; my $name= $1; chomp $name; while(<INPUT_FILE>){ if($_=~ /^TASK\|VALUE = (.*)/){ $task_value= $1; } elsif($_=~ /^(.*) = (.*)/){ $line=$_; } elsif($_=~ /^NAME\|VALUE = (.*)/){ exit; } print "\n$name $task_value $line\n"; } }

And my output is:

a copy a copy CAPS|VALUE = 0 a copy PKG_TYPE|VALUE = premium a copy PKG_TYPE|VALUE = premium a copy NAME|VALUE = z a cut NAME|VALUE = z a cut CAPS|VALUE = 0 a cut PKG_TYPE|VALUE = premium a cut PKG_TYPE|VALUE = premium a cut NAME|VALUE = c a paste NAME|VALUE = c a paste STACK|VALUE = 2 a paste SHIP|VALUE = lowtier

Can someone please help me with subroutine to do the job here? PLease dont hesitate to ask if you got any further questions on this, Regards


In reply to Need your help in a pattern based text manipulation algorithm by kaushik9918

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.