How to find and replace a string by increasing its numerical part?

My input file is,

ami21 ami65
ami67 ami66
ami88 ami76 ami29
ami55 
ami54
ami32

Using a single command line I need the output as,

ami22 ami66
ami68 ami67
ami89 ami77 ami30
ami56
ami55
ami33

I used the command

awk -vRS=ami '{$0=$0+1;ORS=RT}++n' inputfile > outputfile

but I got the outputfile as ami21ami65ami67ami66ami88ami76ami29ami55ami54ami32

i.e. all strings are written in same line and without space. Could anyone suggext me some better command line.


Solution 1:

It might be easier to use perl for this:

$ perl -pe 's/(\d+)/$1+1/eg' foo
ami22 ami66
ami68 ami67
ami89 ami77 ami30
ami56 
ami55
ami33
  • -pe: loop through the file, line by line, printing each line after evaluating the expression
  • s/(\d+)/$1+1/e: match a number (\d+), and replace it with its increment ($1+1), with e being used to tell Perl to evaluate the replacement.

With awk, you have to include the whitespace in the record separator:

$ awk -v RS='[[:space:]]*ami[[:space:]]*' '$0{$0++;}{ORS=RT}1' foo
ami22 ami66
ami68 ami67
ami89 ami77 ami30
ami56 
ami55
ami33

Solution 2:

Apart from the excellent answer provided by "muru" if you want to try something different, here is a Pythonic way:

#!/usr/bin/env python2
import re
with open('file_name.txt') as f:
    for line in f:
        final_list = ['ami' + str(int(value) + 1) for value in re.findall('\d+', line)]
        print ' '.join(final_list)

Solution 3:

Another python solution through re.sub.

$ python3 -c 'import re, sys
with open(sys.argv[1]) as f:
    for line in f:
        print(re.sub(r"\d+", lambda x: str(int(x.group())+1), line), end="")' file
ami22 ami66
ami68 ami67
ami89 ami77 ami30
ami56 
ami55
ami33