Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.

by pvaldes (Chaplain)
on Jan 01, 2015 at 20:12 UTC ( #1111924=note: print w/replies, xml ) Need Help??


in reply to Re: Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.
in thread Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.

If I'm not wrong the question is: how to do a global substitution of a pattern over several files replacing the pattern by $counter and also updating $counter as $counter+1 each time a substitution is done.

I have some doubts about this lines

++$n; $lines[0] =~ s[/4947000219/\K(4947000210+)] [$1+$n]e;

You search for a big number, then discard the first ten digits, capture next digits and replace it by $1+$n in an eval (e) context when $n is a counter. Ok.

The use of \K here also confuses me a little. Can't see the point of doing this here when you aren't really picking up those digits

when \K is reached Perl throws away everything that it has matched up to that point. It means that our replacement won’t affect anything before the \K, because Perl will have forgotten about it

I wonder if you want to say $n++ or $n += 1 instead ++$n

  • Comment on Re^2: Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.
  • Download Code

Replies are listed 'Best First'.
Re^3: Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.
by hemantjsr (Initiate) on Jan 02, 2015 at 12:40 UTC

    Yes Dera your understanding is correct. Could you please help me tell how to do substitution incrementally. As I try to do with \K which seems not working for me, done the changes $n++

    $lines[0] =~ s~/4947000219/\K(4947000210+)~$1+$n~e;
Re^3: Dera Monks, Have create multiple file from one file now I want to match a pattern in each file at the same time do the replacement with adding one to the match pattern.
by hemantjsr (Initiate) on Jan 05, 2015 at 16:17 UTC

    Done some changes now $lines[0] working fine.. thanks for your help /K creating a problem.. But still issue with $line1

    $lines[0] =~ s~/(4947000219)/$1+$n/~e;

    Working fine

    $lines[1] =~ 's{:20140924105028(\d+)}'{ my $tp = Time::Piece->strptime($1, DATE_FORMAT); ($tp+ONE_MINUTE*2*$n)->strftime(DATE_FORMAT);
     }e;

    Error message

    String found where operator expected at ./prog.pl line 33, near "$lines1 =~ 's{:20140924105028(\d+)}'" (Missing operator before 's{:20140924105028(\d+)}'?) syntax error at ./prog.pl line 33, near "$lines1 =~ 's{:20140924105028(\d+)}'" Can't use global $1 in "my" at ./prog.pl line 34, near "($1" syntax error at ./prog.pl line 41, near "}" Bareword "e" not allowed while "strict subs" in use at ./prog.pl line 36.

    As m very new in perl you kind help would be highly appreciated.

      I'll suggest this instead

      my $tp = Time::Piece->strptime($1, DATE_FORMAT); # out the replacement line print "tp is: $tp\n"; # we check $tp values here, just for the record ($tp+ONE_MINUTE*2*$n)->strftime(DATE_FORMAT);

      mmmh... this second line needs a var. Try something like this (untested):

      $tp = $tp+ONE_MINUTE*2*$n; print "tp is now: $tp\n"; # we check $tp again after the changes $lines[1] =~ 's{:20140924105028(\d+)}'{$tp}e;

      I see also a problem here with 's{}'{}e. Delete the "'" --> s{}{}e

      substitution operator is three or four delimiters s/// or s~~~ or s{}{} ... its never s~~ because replacement is missing s~regex~replacement~

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1111924]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2019-10-22 01:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?