开发者

Auto increment a variable in regex

开发者 https://www.devze.com 2023-02-02 14:59 出处:网络
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.

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,)]),....


Regular expressions are for matching strings. They are not for manipulating variables as the matching occurs.

You don't say what language you are using, but I would suggest you find the way to return all the matches within the given string. Once you have that, you can iterate over the list and insert your value for the incrementing count.


Not with sed. Go use something modern:

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

精彩评论

暂无评论...
验证码 换一张
取 消