freelanceprogrammers.org Forum Index » Perl

Search and Replace progarm help needed please


View user's profile Post To page top
kumincam Posted: Fri Mar 18, 2005 7:45 pm


Joined: 18 Mar 2005

Posts: 1
Search and Replace progarm help needed please
Hi there.

I am currently working on a program that uses the `s` operator to
substitute
words within some specified text.

Now I can do this no problem as one program, however, what I really
want to do

is create a general search and replace program that reads in the
substitution

operators from an independant file to do the converting and then any
text file

at for conversion by the read in operators. the converted text could
then be

used as STDOUT or printed to file.

Here is the all in one program:

#!/usr/local/bin/perl -w

$_ = "My cat can get very annoying at times but then that is
to be

expected seeing as he is nearly 80 years old"; # text file

s/can/cn/g; # substition operators
s/get/gt/g;
s/very/vry/g;
s/annoying/annoyN/g;
s/times/x/g;
s/then/thn/g;
s/that/tht/g;
s/to/2/g;
s/be/b/g;
s/expected/Xpectd/g;
s/seeing/C_N/g;
s/80/8T/g;
s/years/yrs/g;

print "$_
";

This is how I want to do it:

At the command line -

perl my_prog.pl my_sub.txt text.txt

where my_sub.txt is

s/can/cn/g
s/get/gt/g
s/very/vry/g
s/annoying/annoyN/g
s/times/x/g
s/then/thn/g
s/that/tht/g
s/to/2/g
s/be/b/g
s/expected/Xpectd/g
s/seeing/C_N/g
s/80/8T/g
s/years/yrs/g

and text.txt is

My cat can get very annoying at times but then that is to be
expected
seeing as he is nearly 80 years old

and my_prog.pl is the script

Any ideas?
I would appreciate any suggestion .
Thanks for your time,

P.S. I`m using a win32 ver of ActivePerl 5.8 on a P4 class laptop
Reply with quote
Send private message
View user's profile Post To page top
golagolu Posted: Sat Mar 19, 2005 1:04 am


Joined: 19 Mar 2005

Posts: 5
Search and Replace progarm help needed please
Below is the sample script. You can start from here

====================
#!/bin/perl

$cmd="s/Hoo/How/g";

$text="Hoo are you";

print "Text before operation--> $text
";
eval "$text=~$cmd";
print "Text after operation--> $text
";
====================


populate the value of $cmd and $text by reading from the respective
files and run the above in the loop for each command.

--- In perl_official@yahoogroups.com, "kumincam" <kumincam@y...>
wrote:
>
>
> Hi there.
>
> I am currently working on a program that uses the `s` operator to
> substitute
> words within some specified text.
>
> Now I can do this no problem as one program, however, what I really
> want to do
>
> is create a general search and replace program that reads in the
> substitution
>
> operators from an independant file to do the converting and then
any
> text file
>
> at for conversion by the read in operators. the converted text
could
> then be
>
> used as STDOUT or printed to file.
>
> Here is the all in one program:
>
> #!/usr/local/bin/perl -w
>
> $_ = "My cat can get very annoying at times but then that is
> to be
>
> expected seeing as he is nearly 80 years old"; # text file
>
> s/can/cn/g; # substition operators
> s/get/gt/g;
> s/very/vry/g;
> s/annoying/annoyN/g;
> s/times/x/g;
> s/then/thn/g;
> s/that/tht/g;
> s/to/2/g;
> s/be/b/g;
> s/expected/Xpectd/g;
> s/seeing/C_N/g;
> s/80/8T/g;
> s/years/yrs/g;
>
> print "$_
";
>
> This is how I want to do it:
>
> At the command line -
>
> perl my_prog.pl my_sub.txt text.txt
>
> where my_sub.txt is
>
> s/can/cn/g
> s/get/gt/g
> s/very/vry/g
> s/annoying/annoyN/g
> s/times/x/g
> s/then/thn/g
> s/that/tht/g
> s/to/2/g
> s/be/b/g
> s/expected/Xpectd/g
> s/seeing/C_N/g
> s/80/8T/g
> s/years/yrs/g
>
> and text.txt is
>
> My cat can get very annoying at times but then that is to be
> expected
> seeing as he is nearly 80 years old
>
> and my_prog.pl is the script
>
> Any ideas?
> I would appreciate any suggestion .
> Thanks for your time,
>
> P.S. I`m using a win32 ver of ActivePerl 5.8 on a P4 class laptop
Reply with quote
Send private message
View user's profile Post To page top
santosh_ess2002 Posted: Mon Mar 21, 2005 10:45 am


Joined: 21 Mar 2005

Posts: 1
Search and Replace progarm help needed please
Dear:

You can do one thing. write a program which ask for

Enter String to search:
Enter String to replace:
Enter file name:

or you can pass it as parameter

filename <search string> <replace string> <file name>

and for coding you can use the tr(translate function) or substitution operator
to make it dynamic. Syntax are as follows:-

Translation
The tr function allows character-by-character translation.

The Translate Operator

tr/string/replacement_string/

tr /A-Z/a-z/;

This can be read as follows: "translate all characters in the range from A to Z
to the corresponding characters in the range from a to z".

The operation is applied to the value of $_, in the current input line. If you
would like it to be applied to the value of a variable $line, you should write

$line =~ tr /A-Z/a-z/;#converts all letters to lower case

$line =~ tr /a-z/A-Z/;#converts all letters to upper case

The following expression replaces each a with e, each b with d, and each c with
f in the variable $sentence. The expression returns the number of substitutions
made.

$sentence =~ tr/abc/edf/

Most of the special RE codes do not apply in the tr function. For example, the
statement here counts the number of asterisks in the $sentence variable and
stores that in the $count variable.

$count = ($sentence =~ tr/*/*/);
Substitution
The Substitution Operator

s/string_to_find/replacement_string/

s /
/<br>/g;

This can be read as follows: "substitute all newline characters with a <br> HTML
tag".

The operation is applied to the value of $_, in the current input line. If you
would like it to be applied to the value of a variable $line, you should write

$line =~ s /
/<br>/g;#converts newlines to <br>

$line =~ s /

/<p>/g;#converts 2 newlines to <p>

Regards,
Santosh



kumincam <kumincam@...> wrote:

Hi there.

I am currently working on a program that uses the `s` operator to
substitute
words within some specified text.

Now I can do this no problem as one program, however, what I really
want to do

is create a general search and replace program that reads in the
substitution

operators from an independant file to do the converting and then any
text file

at for conversion by the read in operators. the converted text could
then be

used as STDOUT or printed to file.

Here is the all in one program:

#!/usr/local/bin/perl -w

$_ = "My cat can get very annoying at times but then that is
to be

expected seeing as he is nearly 80 years old"; # text file

s/can/cn/g; # substition operators
s/get/gt/g;
s/very/vry/g;
s/annoying/annoyN/g;
s/times/x/g;
s/then/thn/g;
s/that/tht/g;
s/to/2/g;
s/be/b/g;
s/expected/Xpectd/g;
s/seeing/C_N/g;
s/80/8T/g;
s/years/yrs/g;

print "$_
";

This is how I want to do it:

At the command line -

perl my_prog.pl my_sub.txt text.txt

where my_sub.txt is

s/can/cn/g
s/get/gt/g
s/very/vry/g
s/annoying/annoyN/g
s/times/x/g
s/then/thn/g
s/that/tht/g
s/to/2/g
s/be/b/g
s/expected/Xpectd/g
s/seeing/C_N/g
s/80/8T/g
s/years/yrs/g

and text.txt is

My cat can get very annoying at times but then that is to be
expected
seeing as he is nearly 80 years old

and my_prog.pl is the script

Any ideas?
I would appreciate any suggestion .
Thanks for your time,

P.S. I`m using a win32 ver of ActivePerl 5.8 on a P4 class laptop










Yahoo! Groups SponsorADVERTISEMENT


---------------------------------
Yahoo! Groups Links

To visit your group on the web, go to:
http://groups.yahoo.com/group/perl_official/

To unsubscribe from this group, send an email to:
perl_official-unsubscribe@yahoogroups.com

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.




---------------------------------
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!

[Non-text portions of this message have been removed]
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help