For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2004 > How do it do find and replace specific words using perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do it do find and replace specific words using perl
Anand V

2004-07-23, 8:56 am

Hi All,

Could some help me in doing this using perl.

1. Open a text file (Adapter.data ) with following contents.

HEADER.JMSDestination=sample_dataQ
HEADER.JMSDeliveryMode=Persistent
HEADER.JMSType=Data
HEADER.JMSCorrelationID=2
HEADER.JMSPriority=4

PROPERTY.STRING.Data_Type=Purchase_Order
PROPERTY.STRING.Data_Qualifier=Update
PROPERTY.STRING.Data_Delivery_Mode=Notification
PROPERTY.STRING.Data_Format=OPEN
PROPERTY.LONG.TimeToLive=200
PROPERTY.STRING.Src_Client_Addr=godavari::SendToAdapter
PROPERTY.STRING.Src_Client_Version=1.0
PROPERTY.STRING.Src_Client_Type=TestHarness

specific.Directory=/home/anand/Demo/ADK/temp/sendToAdapter
specific.RequestTimeOut=5

specific.DataFile=/home/anand/Demo/ADK/temp/sendToAdapter/someFileToSend.dat


2. Replace sample_dataQ with Adapter_dataQ and replace someFileToSend.dat
with data.txt


Any login to do this with brief syntax is highly appreciated.

Thanks in advance

Anand



James Edward Gray II

2004-07-23, 3:55 pm

On Jul 23, 2004, at 5:13 AM, Anand.V@gxs.com wrote:

> Hi All,


Hello.

> Could some help me in doing this using perl.


We will "help", yes, but we probably won't write it for you. What have
you tried. Where are you stuck? Show us some code.

James

Anand V

2004-07-23, 3:55 pm


Hi James,

I was trying this but not sure where it is going wrong ...

use strict;
use File::Copy;

my $dest_file = "sendToAdapter.properties";
my $searchstr = 'sample';
my $repstr = 'FileAdapter';

open(FL, $dest_file) or die("Doh - $!");

$^I = '~';
s/$searchstr/$repstr/g while <FL>;

close(FL);

Would appreciate if you could help me.

Thanks & Regards,
Anand




-----Original Message-----
From: James Edward Gray II [mailto:james@grayproductions.net]
Sent: Friday, July 23, 2004 6:18 PM
To: Anand.V@gxs.com
Subject: Re: How do it do find and replace specific words using perl


On Jul 23, 2004, at 5:13 AM, Anand.V@gxs.com wrote:

> Hi All,


Hello.

> Could some help me in doing this using perl.


We will "help", yes, but we probably won't write it for you. What have
you tried. Where are you stuck? Show us some code.

James

James Edward Gray II

2004-07-23, 3:55 pm

On Jul 23, 2004, at 8:14 AM, Anand.V@gxs.com wrote:

>
> Hi James,


Hello again.

> I was trying this but not sure where it is going wrong ...


There you go. Now I'll help... ;)

> use strict;
> use File::Copy;


You import, but do not use the above module. We don't need it.

> my $dest_file = "sendToAdapter.properties";
> my $searchstr = 'sample';
> my $repstr = 'FileAdapter';
>
> open(FL, $dest_file) or die("Doh - $!");
>
> $^I = '~';


I believe the above trick only works with the <> construct. You're
using a filehandle, so it's not active.

> s/$searchstr/$repstr/g while <FL>;


You are reading in the line and altering it. What you are forgetting
to do it to print it back out.

> close(FL);
>
> Would appreciate if you could help me.


I'll sure try. Here's my corrected version of your script:

#!/usr/bin/perl

use strict;
use warnings; # a good idea, helps us find mistakes easier

push @ARGV, "sendToAdapter.properties"; # add to @ARGV, which <>
processes
local $^I = '~'; # set inplace editing

my $searchstr = 'sample';
my $repstr = 'FileAdapter';

while (<> ) { # read
s/$searchstr/$repstr/g; # change
print; # write
}

__END__

Hope that helps.

James

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com