Concatenating fixed length lines in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating fixed length lines in shell script
# 1  
Old 04-20-2011
Concatenating fixed length lines in shell script

I have a peculiar file with record format like given below. Each line is wrapped to next lines after certain number of characters. I want to concatenate all wrapped lines into 1.

InputSmiliewrapped after 10 columns)
Code:
This is li
ne1
This is li
ne2 and 
this line
is too lo
ng
Shortline

Output:
Code:
This is line1
This is line2 and this line is too long
Shortline

Thanks for help.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 04-20-2011 at 11:49 AM.. Reason: code tags, please!
# 2  
Old 04-20-2011
How do you determine the end of the wrapped line?
# 3  
Old 04-20-2011
New lines start with Capital letters?
Code:
sed '
  :loop
  $b
  N
  s/\n\([^A-Z]\)/\1/
  t loop
  P
  s/.*\n//
  t loop
 ' infile > outfile

This User Gave Thanks to DGPickett For This Post:
# 4  
Old 04-20-2011
We can determine by the length.
If the length of the line is < 10 chars, it is not wrapped and we can ignore it. (line 3 in example) else, we keep concatenating below lines until we encouter a line whose length is < 10 chars.
# 5  
Old 04-20-2011
Quote:
Originally Posted by kmanyam
We can determine by the length.
If the length of the line is < 10 chars, it is not wrapped and we can ignore it. (line 3 in example) else, we keep concatenating below lines until we encouter a line whose length is < 10 chars.

How do you deal with a record of 10 chars though?
Code:
1234567890
Shortlines


Last edited by Skrynesaver; 04-20-2011 at 11:56 AM.. Reason: Fixed formatting
# 6  
Old 04-20-2011
Hope that info was not destroyed upstream! How did it get so chopped?
# 7  
Old 04-20-2011
Quote:
Originally Posted by kmanyam
We can determine by the length.
If the length of the line is < 10 chars, it is not wrapped and we can ignore it. (line 3 in example) else, we keep concatenating below lines until we encouter a line whose length is < 10 chars.
Something like this?
Code:
awk '{ORS=length < 10? RS: x}1;END{if(ORS==RS)print}' file


Last edited by Franklin52; 04-20-2011 at 01:32 PM.. Reason: typo
This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break one long string into multiple fixed length lines

This is actually a KSH under Unix System Services (Z/OS), but hoping I can get a standard AIX/KSH solution to work... I have a very large, single line file in Windows, that we download via FTP, with the "SITE WRAP" option, into a Z/OS file with an LRECL of 200. This essentially breaks the single... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

2. Shell Programming and Scripting

Fixed Length file from a SQL script

Hi, I have a DB2 UDB 9.7 SQL script, as follows: I need to pass the script into Unix and generate a fixed length file from this. Can someone kindly provide a script to achieve it? SELECT CAST(COALESCE(CL_ID,'000000000') AS CHAR(9)) AS CL_ID ,STATUS... (5 Replies)
Discussion started by: ebsus
5 Replies

3. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

4. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

7. Shell Programming and Scripting

reading fixed length flat file and calling java code using shell scripting

I am new to shell scripting and I have to to the following I have a flat file with storename(lenth 20) , emailaddress(lenth 40), location(15). There is NO delimiters in that file. Like the following str00001.txt StoreName emailaddress location... (3 Replies)
Discussion started by: willywilly
3 Replies

8. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

9. Shell Programming and Scripting

Fixed Length records- Korne Shell Program.

Hi, I need some help regarding in writing a Korne shell script, in determining the fixed length records in a data file. We have already utility in place, which does this work. The Code for this is as below. In the below $1 is the parameter passed to the script, which is the data file name. ... (4 Replies)
Discussion started by: nrajesh_2009
4 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question