Parse large file on line count (random lines)

I have a file that needs to be parsed into multiple files every time there line contains a number 1. the problem i face is the lines are random and the file size is random. an example is that on line 4, 65, 187, 202 & 209 are number 1's so there has to be file breaks between all those to create 4 new files. I can find the number 1's using sed and i can create 1 file based on the first line count but not the continuing ones.

Does anyone know how to parse a file based on random lines

Thanks ahead of time

Do you have some example input/output?

I do, its a bit long but this should give the right idea

 MM  PUT BATCH LETTER WRITER LETTERHEAD
 MM  AND CARRIAGE TAPE # Z2551Z11 ON PRINTER FOR
 MM  CLIENTXXXXXXXXX
 MM  SET CHANNEL # 1 FIRST PRINT LINE.
 MMH SET PRINT POSITION # 1 ONE HALF INCH FROM LEFT EDGE OF FORM.
1



 October 11, 2011



 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxx, XX XXXXX



 RE CCCCCCCCC No: XXXXXXX
 Past Due Since: 00-00-00
 Total Amount Past Due: $.00

 Aviso importante para las personas que hablan espanol: Esta
 notificacion es de suma importancia.  Puede afectar su derecho
 a continuar viviendo en su casa.  Si no entiende el continido

1



 October 11, 2011



 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 Xxxxxxxxxxxxxxxxxxxxx, XX XXXXX



 RE CCCCCCCCC No: XXXXXXX
 Past Due Since: 00-00-00
 Total Amount Past Due: $.00

 Aviso importante para las personas que hablan espanol: Esta
 notificacion es de suma importancia.  Puede afectar su derecho
 a continuar viviendo en su casa.  Si no entiende el continido
 notificacion es de suma importancia.  Puede afectar su derecho
 a continuar viviendo en su casa.  Si no entiende el continido
 notificacion es de suma importancia.  Puede afectar su derecho
 a continuar viviendo en su casa.  Si no entiende el continido

1

As you can see the number 1's are at lines 6, 31 and 60 so i would need 2 files created, one from like 6 to 31 and 1 from 31 to 60 and so on

With what filenames?

Any file name would be fine, if it creates the files from there i can do what ever with them, i just am having the issue with the create part. Something in numeric order or random is fine.

currfile=1
while read line
do
   if [ "$line" = "1" ]
   then
      (( currfile++ ))
   else
      echo "$line" >> ${currfile}.txt
   fi
done < infile.txt
1 Like

Thank You! Worked like a champ!!!!