Hi,
I have a file with data in the following format
BOX
-1.000000 -1.000000 0.000000
30.00000 14.00000 0.1000000
0.000000 0.000000 0.000000
0.000000
0.000000
CYLINDER
3.595000 2.995000 0.000000
0.5100000 2.000000
Z
0.000000 0.000000
0.000000
I want to convert these files into the following format instead
_Box -1.000000,-1.000000,0.000000 30.00000 14.00000 0.1000000
_Cylinder 3.595000,2.995000,0.000000 0.5100000 2.000000
Essentially these are the things I have done to re-format it.
- Added "_" before BOX and CYLINDER and then changed these strings to lowercase (apart from the first letter).
- The first line after BOX and CYLINDER moved to the same line as _Box and _Cylinder, but commas are added to separate the three numbers on that line.
- The second line after BOX and CYLINDER moved to the same line as before - but separated using spaces.
- Delete 3rd, 4th and 5th lines after BOX and CYLINDER.
I'd like to do this in perl if possible. So far I have the following script,
$infile="box_cyl.txt";
open(IN,$infile);
@lines=<IN>;
for ($i=1;$i<=$#lines;$i++){
if($lines[$i] =~ 'CYLINDER'){
@line1 = split (/ /,$lines[$i+2]);
@line2 = split (/ /,$lines[$i+3]);
}
elsif($lines[$i] =~ 'BOX'){
@line1 = split (/ /,$lines[$i+2]);
@line2 = split (/ /,$lines[$i+3]);
}
}
Which basically finds the location of BOX and CYLINDER and reads the data in the first and second lines after BOX and CYLINDER is found. I'm new to perl and I'm not sure how to achieve the rest.
Many thanks!