using sed to find and replace multiple numbers

I have looked around and there are several examples of how to use sed, but I don't think any of them help me very much with what I am trying to do.

I have a text file like this....

    1! SRCNAM = 00001  !
    1! X =  50.0000,  0.0000, 25.0000,0.2279E-07,0.0000E+00,0.3697E-07,0.0000E+00,0.0000E+00,0.3212E-08,0.4051E-10  !  !END!
   10! SRCNAM = 00010  !
   10! X =  50.0000,  0.0000, 25.0000,0.2296E-07,0.0000E+00,0.3757E-07,0.0000E+00,0.0000E+00,0.3254E-08,0.4123E-10  !  !END! 
  100! SRCNAM = 00100  !
  100! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2224E-07,0.2836E-09  !  !END!
 1000! SRCNAM = 01000  !
 1000! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2223E-07,0.2836E-09  !  !END!

...and I would like to replace the entry "1" and "00001" with "1001" and "01001", respectively...and replace the entry "10" and "0010" with "1010" and "01010", respectively...and replace "100" and "00100" with "1100" and "01100", respectively...and replace "1000" and 01000" with "2000" and "02000", respectively.

The result I want would look like...

  1001! SRCNAM = 01001  !
  1001! X =  50.0000,  0.0000, 25.0000,0.2279E-07,0.0000E+00,0.3697E-07,0.0000E+00,0.0000E+00,0.3212E-08,0.4051E-10  !  !END!
  1010! SRCNAM = 01010  !
  1010! X =  50.0000,  0.0000, 25.0000,0.2296E-07,0.0000E+00,0.3757E-07,0.0000E+00,0.0000E+00,0.3254E-08,0.4123E-10  !  !END! 
  1100! SRCNAM = 01100  !
  1100! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2224E-07,0.2836E-09  !  !END!
  2000! SRCNAM = 02000  !
  2000! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2223E-07,0.2836E-09  !  !END!

I plan to put the correct code to do this in a shell script and iterate through each line.

Is using sed the right way to go?

Any help is appreciated.

Are you only doing for the lines like:

SRCNAM = 00001

I did not see any "1" as you mentioned in your request; but did see this "00001"

The "1", "10", "100", "1000" all lie in the first few character positions for every line and I would like to change them all in addition to the cases with:

SRCNAM = 00001, SRCNAM = 00010, ect.

Does this answer your question?

Thanks for the help.

Gary.

Are those the only five rules for substitution?

And, from

1! SRCNAM = 00001  !

to this

1000! SRCNAM = 01000  !

does not seem to make sense given your instructions:

Ah, yes, that does not make any sense. I made a mistake in my initial post. I have corrected it.

Gary.

If only those five substitutions, would five sed commands be fine?

There are more lines to change, but I just wanted to get a flavor of the type of commands that I would need to put in a loop in a shell script. Five sed commands would be more than enough to get me going.

Thanks.

Gary.

$ sed 's/   1! SRCNAM = 00001/1001! SRCNAM = 01001/' <sample15.txt
 1001! SRCNAM = 01001  !
    1! X =  50.0000,  0.0000, 25.0000,0.2279E-07,0.0000E+00,0.3697E-07,0.0000E+00,0.0000E+00,0.3212E-08,0.4051E-10  !  !END!
   10! SRCNAM = 00010  !
   10! X =  50.0000,  0.0000, 25.0000,0.2296E-07,0.0000E+00,0.3757E-07,0.0000E+00,0.0000E+00,0.3254E-08,0.4123E-10  !  !END!
  100! SRCNAM = 00100  !
  100! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2224E-07,0.2836E-09  !  !END!
 1000! SRCNAM = 01000  !
 1000! X =  50.0000,  0.0000, 25.0000,0.1563E-06,0.0000E+00,0.2575E-06,0.0000E+00,0.0000E+00,0.2223E-07,0.2836E-09  !  !END!

Thanks. I thought of doing this exact command, but I would need to loop from SRCNAM = 00001 to SRCNAM = 01001 and I wasn't sure if this was possible. I know I could loop from SRCNAM = 1 to SRCNAM = 1001, but the leading zeros complicate what I am trying to do.

Instead, is there a similar sed command that could specify the range of the placement of characters to change?

i.e.

 sed -n 1p| sed 's/^.\(....\)\(.*\)/\1001\2/g' 

Or alternatively try awk?

awk '{$1+=1000} /SRCNAM/{sub(/[0-9]+/,0 $1,$2)}1' FS=\! OFS=\! infile

Thanks scrutinizer! That worked perfectly.