Cut each line based on the character size

Hello All,
I have a file which contain below lines. The starting word of each line is call and the end line is semi colon. I need to find the character size of each line and then move it to a file. If the character size is more than 255 then I need to push that line to a next file and I need to keep on doing this for every 255 characters in a file but I cant break this line because its a index rebuild script

Input:

call sp_rebuildindex('dbo.xxx','column [clean_timezone_string]');
call sp_rebuildindex('dbo.vvvv','column [pkey_best_no_address_2]');
call sp_rebuildindex('dbo.zzzz','column [clean_address_type_code]');
call sp_rebuildindex('dbo.ttttt','column [clean_city_abbreviation]');

Try:

awk 'length>255 || NR==1{close(f); f="newfile" ++c} {print > f}' file

newfile1 is getting created but it contains the same line as input file. Its not creating multiple files.

head -10 indexcreationsql.out2
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [create_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [created_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_code]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [other_desc]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolved_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_log_id]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [program_name]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolve_date]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [last_update_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [procedure_name]');


awk 'length>255 || NR==1{close(f); f="newfile" ++c} {print > f}' indexcreationsql.out2
head -10 newfile1
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [create_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [created_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_code]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [other_desc]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolved_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_log_id]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [program_name]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolve_date]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [last_update_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [procedure_name]');

But none of those lines in your sample have a length > 255 . If that is not what you mean, than what does "If the character size is more than 255" mean?

We need to append each line to a file. If the characters of the appended line is more than 255 then it should create another file but the line should not break. For eg: file:1 contains only 227 characters if we add the next line then it will be more than 255 and at the same time all the line should have call command in order to execute the index rebuild.

Eg:

head -10 indexcreationsql.out2
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [create_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [created_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_code]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [other_desc]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolved_by]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_log_id]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [program_name]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolve_date]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [last_update_dt]');
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [procedure_name]');

Output:
file:1

call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [create_dt]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [created_by]');

file:2

call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [other_desc]');call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolved_by]');call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_log_id]');

Wouldn't it have been nice and time saving for all, had you specified that in the first place?

Try

split -C255 file
for FN in x*; do paste -sd" " $FN > $FN.new; done

stat -c"%n %s" *.new
xaa.new 227
xab.new 231
xac.new 236
xad.new 80

cf x*.new
xaa.new:
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [create_dt]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [created_by]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_code]');
xab.new:
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [other_desc]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolved_by]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [error_log_id]');
xac.new:
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [program_name]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [resolve_date]'); call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [last_update_dt]');
xad.new:
call sp_iqrebuildindex('DTC_TRANS.dtc_error_log_vw','column [procedure_name]');

Thanks and it worked :):):slight_smile: