Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert fixed-width file to a pipe delimited file 1

Status
Not open for further replies.

mwesticle

Programmer
Nov 19, 2003
51
US
Hi All...
I have what is probably a very easy question for the experts that patrol this board...

I have a fixed-width file with four 10-byte fields (field 1 = bytes 1-10, field 2 = bytes 11-20, field 3 = bytes 21-30, field 4 = bytes 31-40). Some of the fixed-width fields contain no spaces, or 'unused' bytes (as in line one of the input below), some contain spaces in the last bytes of the field (as in line two of the input below), and some contain spaces between bytes of data within the field (as in line three of the input below).I need to use nawk, or some other unix utility (sed, etc.) to convert it into a pipe-delimited file. I don't want any actual data truncated, except for trailing spaces.

So, the input looks like this (without the 'ticks'):

'1234567890123456789012345678901234567890'
'12345 1234567 12 1234567890'
'123 6789012345 78 12345 1234567890'

I need the output to look like this (without the 'ticks'):

'1234567890|1234567890|1234567890|1234567890'
'12345|1234567|12|1234567890'
'123 67890|12345 78|12345|1234567890'

I'd like to eventually put this into a ksh script, if that makes any difference. As always, any help would be GREATLY appreciated!

 
Something like this ?
nawk '{
for(i=0;i<4;++i){
a=substr($0,1+10*i,10);sub(/ *$/,"",a)
printf "%s"(i<3 ? "|" : "\n"),a
}}' /path/to/input >output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Yes, I think that could work... What would this nawk look like if the fixed-width fields were variable length, meaning field one was bytes 1-6, field two was bytes 7-20, field three was bytes 21-35, and field three was bytes 36-40? Know what I mean? What if they weren't all exactly ten bytes???
 
field one was bytes 1-6, field two was bytes 7-20, field three was bytes 21-35, and field 4 was bytes 36-40
Something like this ?
nawk 'BEGIN{j=split("1 7 21 36 41",s)}
{for(i=1;i<j;++i){
a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a)
printf "%s"(i<j-1 ? "|" : "\n"),a
}}' /path/to/input >output

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I think we're really close to getting there. I'm getting a syntax error... It seems to have a problem with "printf". What do you think?

nawk: syntax error at source line 1
context is
BEGIN{j=split("1 7 21 36 41",s)}{for(i=1;i<j;++i){ a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a) >>> printf <<<
"%s"(i<j-1 ? "|" : "\n"),a}}
nawk: illegal statement at source line 1
 
Replace this:
printf "%s"(i<j-1 ? "|" : "\n"),a
By this:
printf a;if(i<j-1)printf "|";else printf "\n"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
OK, I'm now giving it the command:

nawk 'BEGIN{j=split("1 7 21 36 41",s)}{for(i=1;i<j;++i){ a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a) printf a;if(i<j-1)printf "|";else printf "\n"}}' file.dat > file.dat.dlmt

I am getting pretty much the same syntax error:

nawk: syntax error at source line 1
context is
BEGIN{j=split("1 7 21 36 41",s)}{for(i=1;i<j;++i){ a=substr($0,s,s[i+1]-s);sub(/ *$/,"",a) >>> printf <<<
a;if(i<j-1)printf "|";else printf "\n"}}
nawk: illegal statement at source line 1

I know we're close! HELP! Thanks!
 
You can retry previous 2 posts, BUT when you join lines of code in an awk program you have to insert a ; (semi-colon) between them. Lack of one before the printf.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top