0

This question already has an answer here:

My input file is,

ami41 ami56
ami23
ami67
ami45 ami34 ami88
jama66
tumi234 bichi23455
susami345

Using a single command line I need the output as,

ami42 ami57
ami24
ami68
ami46 ami35 ami89
jama66
tumi234 bichi23455
susami345

I used the command,

perl -pe 's/(\d+)/$1+1/eg' inputfile

But I got the output file as,

ami42 ami57
ami24
ami68
ami46 ami35 ami89
jama67
tumi235 bichi23456
susami346

i.e. all numerics with all types strings are increased. But I need the numerical only with ami string to be increased. Could anyone kindly suggest me some better command line.

marked as duplicate by Eliah Kagan, karel, Eric Carvalho, David Foerster, αғsнιη Apr 2 '15 at 17:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Use a Perl lookbehind:

$ perl -pe 's/(?<=ami)(\d+)/$1+1/eg' inputfile
ami42 ami57
ami24
ami68
ami46 ami35 ami89
jama66
tumi234 bichi23455
susami346

The (?=<ami) tells Perl to look for ami before matching the rest of the pattern, but not actually include it in the match.

  • It is useful. Thanks. But I need "susami345" in the output file where this 'perl' command is giving "susami346". Can you please give some modified version of 'perl' command which can handle only "ami" string? – tamal Feb 25 '15 at 10:51
  • @tamal just add a word boundary marker: \bami instead of ami. – muru Feb 25 '15 at 11:00
  • If I want to replace the string "ami007" to "ami008" by your 'perl' command. But it is actually replacing "ami007" to "ami8". Can you kindly suggest some solution ? – tamal Feb 26 '15 at 8:38
  • Instead of (?<=ami)(\d+) use (?<=ami0*)[1-9]\d*. – muru Feb 26 '15 at 8:44
  • This says that "Variable length lookbehind not implemented in regex m/(?<=ami*)(\d*)/ at -e line 1. – tamal Feb 26 '15 at 9:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.