if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING.

Thanks in advance. This is ultimately to grab info from serial numbers, like production date and correspond machines with lease numbers.

This starts the process...

$ echo W87151WR71C
W87151WR71C

$ echo W87151WR71C | cut -c4-5
15

$ testval=`echo W87151WR71C | cut -c4-5`

$ echo $testval
15
ORIGSTRING=W87551WR71C
MODSTRING=`echo "$ORIGSTRING" | awk -v FS='' -v OFS='' '$4$5 > 45 {++$3; print}'`

NOTE: While splitting a record into characters when FS is null isn't part of the POSIX standard, it's an extension that's implemented in the two versions of AWK that i tested (nawk and gawk). If this doesn't work for you, perhaps your version of AWK doesn't support it.

echo "W87461WR71C" | \
awk '{x=substr($0,3,1);if(substr($0,4,2) > 45) print substr($0,1,2)""++x""substr($0,4);else print}'

JoeyG, I already got that far, but no farther..'

alister, looks good except I need it to echo origstring unaltered if the 4th and 5th < 45, your solution echos blank if the 4th and 5th characters arent greater than 45.

To include printing of unaltered strings:

ORIGSTRING=W87551WR71C
MODSTRING=`echo "$ORIGSTRING" | awk -v FS='' -v OFS='' '$4$5 > 45 {++$3} {print}'`

If you show what you have done, it makes it easier for others to assist and advise. I assumed you hadn't even gotten that far.