Help with script to add two zeros to certain lines in a set of files?

Hello... I should be better with scripting but I am not so turning here for some help. I did search quite a bit using google and didn't find anything meeting my needs for this. What am after here is a script (in a redhat linux env) that will read in a small series of files (netbackup vault reports) and add two zeros to the beginning of any line segment that has an L2 or L4 in it.. this is what I came up with to augment the part of the files

cat *.rpt | egrep "(L2)|(L4)" | awk '{t="00"}{ print t""$1 }'

this works but if only digs out the part of the file to be edited. I need to edit the files adding the zeros while preserving the original file names and other file context. I searched around here and found another script and tried to merge my line with that script but it manged the test files.. ugh. Here is what I had..

for i in *.rpt;do cat $i | egrep "(L2)|(L4)" | awk '{t="00"}{ print t""$1 }' $i > ${i}_ && mv -f ${i}_ $i;done

it added the zeros but did it to each line and ripped the files apart (deleted everything after the first line of the file).. Anyways.. Can someone offer some me some help on this? Thanks much!

Yes,
please post a small sample of your input and an example of the expected/desired output.

Oh thanks so much for responding.. and so quickly!

here is the input file

here is how I would like the outcome to look

So I want to add two zeros to the beginning of the media ID for the tapes. Each file segment (media ID) I want to change ends in either an L2 or an L4... so those are the identifiers I was using.. but perhaps there is a better way to do this?
Thanks so much for your time.

perl -i.orig -pe'
  s/(\d*L[24])/00$1/
  ' file1 [.. file2]
1 Like

wow... I had a feeling it would be a small bit of code. Thanks so much.
If I want to have it run on 4 files in the same directory all ending in .rpt do I use a 'for' 'next' or 'while' 'do' type loop? Could you help me with the perl syntax for that?

again, many thanks for your help!!

edit: basically I have 4 new files each week generated that I will cron this script to edit. So each week I will run it against 4 new files, then ftp them (got the script for that done) and then move the files to a "sent" dir once it's all done. If I can get the auto editing bit done I think I can sort the rest on my own. Thanks!

Just pass the filenames to the Perl script:

perl -i.orig -pe'
  s/(\d*L[24])/00$1/
  ' *.rpt

P.S. The script backs up the original versions first, if you don't need that just remove the .orig part:

perl -i -pe ...
1 Like

Oh.. *doink* I see. thanks again for your speedy help on this.

edit: worked like a charm!