Append each line based upon the character size

I have a huge file which contains multiple lines. It need to check whether character length is not more than 255 each line. If its not then it should remove the
character up to column . I have described in the output below. If its more than that
the next line should start with call but if the table name varies then it should not merge to the same line. The output should go to the 4 different file based upon the table name in the 3rd column below. 5th table in the list should again go the first file.

Input:

call mq_dbrebuildindex('SCS.services_HIST','column [end_dt]');
call mq_dbrebuildindex('SCS.services_HIST','column [status]');
call mq_dbrebuildindex('SCS.services_HIST','column [survey]');
call mq_dbrebuildindex('SCS.services_HIST','column [file_id]');
call mq_dbrebuildindex('SCS.services_HIST','column [opt_out]');
call mq_dbrebuildindex('SCS.services_HIST','column [start_dt]');
call mq_dbrebuildindex('SCS.services_HIST','column [dw_load_dt]');
call mq_dbrebuildindex('SCS.services_HIST','column [pip_brands]');
call mq_dbrebuildindex('SCS.services_HIST','column [finalize_dt]');
call mq_dbrebuildindex('SCS.services_HIST','column [scs_service]');
call mq_dbrebuildindex('SCS.services_HIST','column [pharmacy_ncpdp]');
call mq_dbrebuildindex('SCS.services_HIST','column [contract_term_sheet]');
call mq_dbrebuildindex('SCS.reporting_opportunities','column [file_id]');
call mq_dbrebuildindex('SCS.reporting_opportunities','column [fnlndbs]');
call mq_dbrebuildindex('SCS.reporting_opportunities','column [payer_id]');
call mq_dbrebuildindex('SCS.reporting_opportunities','column [pip_edit]');

Output:
File:1

call mq_dbrebuildindex('SCS.services_HIST','column [end_dt]) column [status] column [survey] column [file_id]) column [opt_out] column [start_dt] column [dw_load_dt] column [pip_brands] column [finalize_dt] column [scs_service] column [pharmacy_ncpdp]');
call mq_dbrebuildindex('SCS.services_HIST','column [contract_term_sheet]');

File:2

call mq_dbrebuildindex('SCS.reporting_opportunities','column [file_id] column [fnlndbs]column [payer_id]column [pip_edit]column [client_id]column [person_id]column [rx_number]column [copay_tier]column [dw_load_dt]column [patient_id]column [program_id]')
call mq_dbrebuildindex('SCS.reporting_opportunities','column [billing_ndc]column [days_supply]column [employer_id]column [fill_number]column [claim_number]column [client_id_cd]column [pharmacy_npi]column [processed_dt]column [date_of_birth]')
call mq_dbrebuildindex('SCS.reporting_opportunities','column [employer_name]column [latest_fax_id]column [prescriber_id]column [record_source]column [patient_gender]column [pharmacy_ncpdp]column [transaction_cd]column [date_of_service]');
call mq_dbrebuildindex('SCS.reporting_opportunities','column [date_rx_written]column [earliest_fax_id]column [patient_pay_amt]column [transaction_type]column [dispense_event_id]column [dispensing_status]column [patient_gender_cd]');

File:3

File:4

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules.

If you did not post homework, please:

  1. explain the company you work for and the nature of the problem you are working on,
  2. tell us what operating system you're using,
  3. tell us what shell you're using,
  4. show us what code you have written to try to solve this problem on your own,
  5. explain how four columns specified in your sample input for table SCS.reporting_opportunities is supposed to magically be turned into over thirty columns of sample output, and
  6. explain how your code is supposed to determine which format of output is supposed to be produced based on the name of the table being processed. (Note that in your sample output, columns for table SCS.services_HIST have a <space> in the output before each column string; but in the output for table SCS.reporting_opportunities , there is no <space> before column after the closing square brackets.)

If you did post homework in the main forums, please review the guidelines for posting homework and repost.