On Aug 22, 2012, at 8:32 AM, lina wrote: > Hi, > > I have a data file, wish to split into 9 small files, each one starts > > > 2:MODEL 1 > 1552:ENDMDL > > 1554:MODEL 2 > 3104:ENDMDL > > 3106:MODEL 3 > 4656:ENDMDL > > till > > 12418:MODEL 9 > 13968:ENDMDL > > > The left number are the line numbers. > > I can get those 9 output file via some sed commands, but here I wish to > try using perl. > > The output 9 files and origin files first_9 with the not-working perl > file had been pushed to https://github.com/linanil/Temp
That Perl program is only 19 lines, so you may as well include it in your email. I would recommend doing the following: 1. Open the input file 2. Read each line in the input file 3. Define a "write" flag variable that is true if an output file is open, false if not. 4. If the line read from the input file starts with 'MODEL', open an output file and set the write flag to true. 5. If the line read from the input file starts with 'ENDMDL', close the output and reset the write flag. 6. If neither conditions described in 4. and 5. is true, and the write flag is true, write the input line to the output file. Be sure to use a different name each time you open an output file or you will overwrite the files and end up with only one. You can use the model number extracted from the MODEL line: if( $line =~ m{ \A MODEL \s* (\d+) }x ) { $model_number = $1; // open output file with name incorporating $model_number ... Good luck! -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/