Cut each line based on the character size


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Cut each line based on the character size
# 1  
Old 04-04-2017
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:

Code:
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]');

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules).

Last edited by Don Cragun; 04-04-2017 at 09:48 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 04-05-2017
Try:
Code:
awk 'length>255 || NR==1{close(f); f="newfile" ++c} {print > f}' file

# 3  
Old 04-05-2017
newfile1 is getting created but it contains the same line as input file. Its not creating multiple files.

Code:
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]');


Last edited by Scrutinizer; 04-05-2017 at 12:40 AM..
# 4  
Old 04-05-2017
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?
# 5  
Old 04-05-2017
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:

Code:
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
Code:
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
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]');



Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-05-2017 at 04:45 AM.. Reason: Added CODE tags.
# 6  
Old 04-05-2017
Wouldn't it have been nice and time saving for all, had you specified that in the first place?

Try
Code:
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]');

# 7  
Old 04-05-2017
Thanks and it worked SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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... (1 Reply)
Discussion started by: JoshvaPeter
1 Replies

2. UNIX for Dummies Questions & Answers

Cut command doesn't remove (^C) character from only first line

I have a file which has first 2 junk characters(X^C) at beginning of each line in file. When i run cut -c 2- filename it removes junk characters from all lines except on first line it keeps one junk character control C(^C). Not sure why it is not removing only from first line. (2 Replies)
Discussion started by: later_troy
2 Replies

3. UNIX for Dummies Questions & Answers

Looking for command line to find dirs based on size and date

Hi, My first time on this site, please excuse me if I've come to the wrong forum. I'm fairly new to Unix/Linux and hoping you can help me out. I'm looking for a command line that will return a list of directories that are larger than 50M and older than 2 days. I thought it may be... (6 Replies)
Discussion started by: Wisconsingal
6 Replies

4. Shell Programming and Scripting

cut a line into different fields based on identifiers

cat fileanme.txt custom1=, custom2=, userPulseId=3005, accountPolicyId=1, custom3=, custom4=, homeLocationId=0, i need to make the fields appear in next line based on identifier (,) ie comma so output should read cat fileanme.txt custom1=, custom2=, userPulseId=3005, ... (8 Replies)
Discussion started by: vivek d r
8 Replies

5. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

6. Shell Programming and Scripting

cut a line after a character

hello! i'd like to ask if there's a way using sed in order to cut every character of a line after a certain character.My output looks like this: blabla blavbla blabla # bla bla bla and i want it to be : blabla blavbla blabla thanks in advance (5 Replies)
Discussion started by: vlm
5 Replies

7. UNIX for Advanced & Expert Users

cut words based on the word count of a line

I would like to cut words based on the word count of a line. This over here inspired me with some ideas but I wasn't able to get what I needed. https://www.unix.com/shell-programming-scripting/105841-count-words-each-line-file-using-xargs.html If the line has 6 words I would like to use this.... (8 Replies)
Discussion started by: cokedude
8 Replies

8. Shell Programming and Scripting

Cut multiple data based on character position

How to extract multiple data based on character position. I need to fetch from 7-9 and 22-26 and there is no delimiter for 22-26 since it is part of the column. The file may have more than 1000 character long.I managed to pull any one but not both for example test data 12345 zxc vbnmlk... (1 Reply)
Discussion started by: zooby
1 Replies

9. Shell Programming and Scripting

Read line based on character,

Hi Experts, I have called file1.txt contains below CREATE TABLE "IHUBDEV2"."TLM_BREAK_RULES" ( "OID" VARCHAR2(32) NOT NULL ENABLE, "TLM_PAY_CLASS_OID" VARCHAR2(32) NOT NULL ENABLE, "PUNCHED_BREAKS" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE, "NORMAL_BREAKS"... (3 Replies)
Discussion started by: naree
3 Replies

10. UNIX for Dummies Questions & Answers

delete a line based on first character of the line

Hi, I need to delete all lines in a file which starts with "|" character. Can some one assist me? Thanks (2 Replies)
Discussion started by: borncrazy
2 Replies
Login or Register to Ask a Question