simple question reag search and replace

S

sivga

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
 
G

Gunnar Hjalmarsson

sivga said:
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
 
J

John W. Krahn

sivga said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top