0

I have a string like X1.7_RC02.20170811110948 and I need to increase only the number between RC and the next point, example:

Original string:

X1.7_RC02.20170811110948

Incremented string:

X1.7_RC03.20170811110948

How can I increase in 1 (or more this value)?

4 Answers4

4

with GNU awk for the 3rd arg to match():

$ awk 'match($0,/(.*RC)([^.]+)(.*)/,a){$0=sprintf("%s%02d%s",a[1],a[2]+1,a[3])} 1' file
X1.7_RC03.20170811110948
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

With GNU sed

sed -r 's/(.*)(RC0?)([1-9]+)(\..*)/echo "\1\2$((\3+1))\4"/e' <<<X1.7_RC02.20170811110948
Paul
  • 1,630
  • 1
  • 16
  • 23
1

Considering your data is same as shown example then try with following awk once too and let me know if this helps you.

awk '{val=$0;gsub(/.*RC|\..*/,"",val);val=sprintf("%02d",++val);sub(/RC[0-2]+/,"RC"val);print}'  Input_file

Or if you have a string then you could print it's value and could run above command like:

echo "$var" | awk '{val=$0;gsub(/.*RC|\..*/,"",val);val=sprintf("%02d",++val);sub(/RC[0-2]+/,"RC"val);print}' 
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

awk solution:

Initial string:

s="X1.7_RC02.20170811110948"

awk 'BEGIN{ FS=OFS="_RC"}{ n=substr($2,1,2); print $1,sprintf("%02.f",n+1) substr($2,3)}' <<< $s

The output:

X1.7_RC03.20170811110948
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105