Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple question reag search and replace

Reply
Thread Tools

simple question reag search and replace

 
 
sivga
Guest
Posts: n/a
 
      11-07-2007
Hi all i need to search for a particular string and replace it in a
file

for eg :input file has

stock 3 99999 6 13456000 -market 1 100 5600 shift 2 rshit 36
stock 300 99999 6 13456000 -market 2 300 5700 shift 2 rshit 312
stock 31234 99999 6 13456000 -market 3 400 5700 shift 2 rshit 3675
stock 456 99999 6 13456000 -market 4 200 5800 shift 2 rshit 30

......

99999 in the above lines needs to be replaced with 77777 so the o/p
after replace will be

stock 3 77777 6 13456000 -market 1 100 5600 shift 2 rshit 36
stock 300 77777 6 13456000 -market 2 300 5700 shift 2 rshit 312

How do i do this ?

This does not seem to work ..

while ($line = <AG>) {

if ($line =~ /stock[ ]+([99999])*/ ) {
print " The stock is $line";
$line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;

}
}


Thanks for the help

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-07-2007
sivga wrote:
> while ($line = <AG>) {
>
> if ($line =~ /stock[ ]+([99999])*/ ) {


if ( $line =~ /stock.+\b99999\b/ ) {

> print " The stock is $line";
> $line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;


$line =~ s/\b99999\b/77777/;

Where did you get the idea of using square brackets that way?

Have you seen the extensive Perl documentation on regular expressions?

perldoc perlrequick
perldoc perlreref
perldoc perlretut
perldoc perlre

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-07-2007
sivga wrote:
>
> Hi all i need to search for a particular string and replace it in a
> file
>
> for eg :input file has
>
> stock 3 99999 6 13456000 -market 1 100 5600 shift 2 rshit 36
> stock 300 99999 6 13456000 -market 2 300 5700 shift 2 rshit 312
> stock 31234 99999 6 13456000 -market 3 400 5700 shift 2 rshit 3675
> stock 456 99999 6 13456000 -market 4 200 5800 shift 2 rshit 30
>
> .....
>
> 99999 in the above lines needs to be replaced with 77777 so the o/p
> after replace will be
>
> stock 3 77777 6 13456000 -market 1 100 5600 shift 2 rshit 36
> stock 300 77777 6 13456000 -market 2 300 5700 shift 2 rshit 312
>
> How do i do this ?
>
> This does not seem to work ..
>
> while ($line = <AG>) {
>
> if ($line =~ /stock[ ]+([99999])*/ ) {


Anything between [ and ] is a character class and any duplicate
characters are discarded so [99999] and [9] and [9999999999999999] all
do exactly the same thing. Your pattern says that there are only spaces
between 'stock' and '99999' but your data says that there is a number
between them as well.


> print " The stock is $line";
> $line =~ s/stock[ ]+([99999])*/stock[ ]+([77777])*/;


The right hand side of the substitution is just a string *not* a regular
expression.


> }
> }




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
get back my simple little string after re search and replace Gontrand Trudau Python 0 01-13-2010 05:12 AM
| SEO , Search Engine Optimizer, SEARCH OPtiMIzAtIoN with SeaRch OPtiMizer optimizer.seo@gmail.com Digital Photography 0 04-22-2007 04:20 AM
search and replace question - help! jac Perl Misc 7 05-23-2006 11:52 PM
simple string search and replace Kun Python 4 03-26-2006 04:15 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57