simple question reag search and replace

simple question reag search and replace

am 07.11.2007 23:56:17 von 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 = ) {

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

}
}


Thanks for the help

Re: simple question reag search and replace

am 08.11.2007 00:33:57 von Gunnar Hjalmarsson

sivga wrote:
> while ($line = ) {
>
> 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

Re: simple question reag search and replace

am 08.11.2007 00:52:42 von krahnj

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 = ) {
>
> 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

Re: simple question reag search and replace

am 08.11.2007 00:52:46 von Jim Gibson

In article <1194476177.554213.74490@57g2000hsv.googlegroups.com>, 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 = ) {
>
> if ($line =~ /stock[ ]+([99999])*/ ) {

Your regex does not match because there is a number (e.g., '3') between
'stock' and '99999'.

The '[' and ']' characters create a 'custom character class' that will
match any single character that is part of the class. If you are only
trying to match a space, there is no need to enclose the space ([ ]).

[99999] is a character class comprising the character 9, and is
therefore the same as [9].

If you want to capture all the members of a character class, put the
'*' inside the parenthesis. ([9])* captures only the _last_ 9.

There is no need to capture anything because you do not use what has
been captured.

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

The right side of the substitution operator is not a regular
expression. Therefore, the characters "[]+()*" are plain characters and
will added to the replacement string.

>
> }
> }

You probably want to do something like this:

while() {
s/99999/77777/;
}

or, if that is not specific enough, this will work on your sample input:

while() {
s/stock(\s+\d+\s+)99999/stock${1}77777/;
}

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com