Thanks Bipin :).. Though My Requirement is quite big.
I mean.. Suppose I have 1500 character data length for say 100 columns file and having length for all columns (as defined earlier example - not fixed) out of which -
say 1200 character are fixed length for say 70 columns and remaining 30 columns are having variable length. Now as mentioned in previous example -
Maximum length of all columns will be 1500
and least will be 1200.
Now I have to separate all these 100 columns with a delimiter and require some automation (Cause I have a situation where I need to separate 900 columns), as defining substring every time doesn't seems feasible.
By Variable length I mean.. Like in previous example I taken three number, so some one may have three different phone number and someone may have none...
Thanks Rdrtx1 for your efforts I am newbie on Unix.. Basically a Datawarehouse guy.. So again thanks..
BTW do we require to create specific schema file in this scenario.
Okay..it would be really great if you can please share sample schema file for the example that taken earlier.. "pardon me if i'm asking too much"
---------- Post updated at 02:17 PM ---------- Previous update was at 02:10 PM ----------
One more thing would require your input... Will the script that you mentioned be able to put comma (delimiter) for field which for which no data is there example -
printf can take max length aruments. But it feels like a perl or C/C++/JAVA level task. But bash has substring: ${parameter:offset:length}, so just read the lengths into two arrays: length and offset, and go through them taking apart every line.
The inner loop of any solution is a for every line for n = 0..max print substring of line offset[n] length[n]; if n = max then print lsep, break else print fsep continue. Make sure your solution is stable for short lines (truncates any partial field, missing fields all go blank). Setup will be to take in the field length file and set offset[0..n] and length [0..n] using length. It can be argued that recalculating offset on the fly might be faster than fetching it from an array. Try both and see, if speed is still an issue. I would write it in C as a state machine copying one byte at a time c = getchar(); . . . putchar( c ) and inserting field separators as lengths are exhausted. That is about as fast as it gets.
You are correct, Tis working beautifully and outcome is same as you mentioned, I was using cygwin as my Unix server was down, I tried it with Putty after server was up today and as mentioned "Working Beautifully".