Search code examples
regexvariablesauto-increment

Auto increment a variable in regex


I am using a Unix.sh file and in the file I have a regex which has a variable, that has to be incremented every time.The count is set to 1. Every-time it should increment. stringA=echo $stringA | sed "s/[A-Za-z]\{3\},[0-9]*/$count++,&,/g"

So my output should be something like

prgm([(1,ABC,1,),(2,XYZ,1,),(3,PQR,1,)]),....

but what I get is

prgm([(0++,ABC,1,),(0++,XYZ,1,),(0++,PQR,1,)]),....


Solution

  • Not with sed. Go use something modern:

    perl -pe 's/[A-Z]{3},\d*/$count++.",$&,"/egi'