Scripting a global find and replace in an VME output print file

Hi Folks,
Below is an extract from a VME Print file which gets handed over to a print house. The problem I have is not that tricky rther looking for a way to handle it in a simple and clean way.

Is to first select all lines with "0058" which have four spaces so "0058 " as the selcetion criteria. Only recodrs starting with 0058 and having a max length of 8 characters which the last 4 are spaces need be selected.

Once you have these replace the sapces with a code can be anything for 1 character plus 3 sapces to any combination up to 4 characters

Below is an example of the file it should be straight forward but the 0058 pattern can be found in other places in the file but shouldn't be selecetd

Any ideas and help gratefully accepted.

Gary

0042               ------------------------------------------------------
0058    
0072  558
0073  108       5060156962K                                        0.70

Running through the file performing the following substitution should work.

perl -pi.bak -e 'BEGIN{$replacement="1 2 ";}s/^(0058) {4}$/$1$replacement/' source.vme

Thnaks for the input, I was going down the awk route but like the Perl script looks quite clean

Hi all
I tried the perl but came up with something even simpler, but how to make this handle multiple files so for a given dircetory select all files and process each in turn cerating a new output file for each, any ideas?

sed 's/\(^0058*\)..../\1ABCD/' filename >new_filename

will replace 4 spaces with ABCD