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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.