LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need help in perl. new to it (http://www.linuxquestions.org/questions/linux-newbie-8/need-help-in-perl-new-to-it-801094/)

bigbigwhitecat 04-09-2010 02:57 PM

need help in perl. new to it
 
Hi

I need to write a perl script which takes input as follows:-

perl abc.pl <input_file file1.log> <output_file file2.xml> <stringx1> <to be replaced by string y1> <stringx2> <to be replaced by string y2> .... <stringx.n> <to be replaced by string y.n>

logic:- file1.log contains some lines of text.
the stringx1 in file1.log gets replaced by string y1
the stringx2 in file1.log gets replaced by string y2
.
.
.
.
.
.
the stringxn in file1.log gets replaced by string yn
the new file file2.xml is created with the modifications..

Can you please help me with this?

Regards,
Anurag

troop 04-09-2010 03:15 PM

Code:

#!/usr/bin/perl -w
open (F1,"<".$ARGV[0]);
open (F2,">".$ARGV[1]);
my $ARGC=scalar(@ARGV);
while(<F1>)
{
    my $str = $_;
    my $i=0;
    for($i=2;$i<$ARGC;$i+=2) {
      $str =~ s/$ARGV[$i]/$ARGV[$i+1]/g;
    }
    print F2 $str;
}
close(F2);
close(F1);


Quakeboy02 04-09-2010 03:24 PM

You're free to post what you want, of course, but he gets no benefit if you do his homework for him.

TB0ne 04-09-2010 03:47 PM

Quote:

Originally Posted by Quakeboy02 (Post 3930230)
You're free to post what you want, of course, but he gets no benefit if you do his homework for him.

I agree totally.

'Help' usually means the person asking does SOME part of the work.

bigbigwhitecat 04-09-2010 04:42 PM

Quote:

Originally Posted by TB0ne (Post 3930248)
I agree totally.

'Help' usually means the person asking does SOME part of the work.

sorry people, but i do not know perl at all...sorry for asking, if it was wrong to ask.

bigbigwhitecat 04-09-2010 04:57 PM

Hi troop

Thanks for the help.. this is an excellent script.. works exactly as desired.. thanks once again..

u saved me man..

Regards,
Anurag

TB0ne 04-09-2010 05:29 PM

Quote:

Originally Posted by bigbigwhitecat (Post 3930312)
sorry people, but i do not know perl at all...sorry for asking, if it was wrong to ask.

Not wrong to ask for HELP at all. We're very glad to help anyone who needs it. But if you want a script, you have to demonstrate that you're actually doing some work, not expecting someone to write it for you.

cola 04-09-2010 06:52 PM

Quote:

Originally Posted by bigbigwhitecat (Post 3930312)
sorry people, but i do not know perl at all...sorry for asking, if it was wrong to ask.

Do some google about perl.


All times are GMT -5. The time now is 10:05 PM.