How to modify a field in few lines in a file and save the new file - in Perl
Hi,
I have a small file the contents of which look like this:
########
Line x
PCC a b 10.0 d
PBB a b 12.3 d
PDD a b 14.5 d
Line x+4
########
The lines I am interested in starts with 'P' - here line x+1, x+2 and x+3 - these 3 lines.
I want to do the following things:
1. Go to the first line starting with 'P' (capital P, case-sensitive)
2. Read the 4th field (here 10.0), multiply it by (11/6) and then take the nearest number divisible by 4. For example, here 10*11/6 = 18.33. So I want to round it to 20, which is the nearest number divisible by 4. If it falls in between, say 18 (equidistant from 16 and 20) - I want to round UP to 20 (instead of setting it to 16).
3. Replace this 4th field by the new number.
4. Continue to next lines starting with 'P' and repeat the above operation on the 4th field.
5. If there is no more line starting with 'P' we are finished - save the file (it does not matter if it overwrites the original file)
6. I have a folder where I have multiple files with this same format. I want to do this operation on all the files in that folder at one step instead of doing it again and again for all the files.
I am new in Perl. Trying to learn some quick stuffs. Can anybody help me here please?
I would really appreciate any help.
Thanks.
|