The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 9 12-04-2008 04:04 PM
how to convert Fixed length file to delimited file. satyam_sat Shell Programming and Scripting 7 04-03-2008 02:41 AM
fixed record length george_ Shell Programming and Scripting 16 03-28-2006 05:41 AM
convert XML file into Text file(fixed length) ram2s2001 Shell Programming and Scripting 0 11-03-2005 01:28 AM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 01:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-25-2005
nelson553011 nelson553011 is offline
Registered User
  
 

Join Date: Sep 2004
Location: Minnesota
Posts: 13
Convert delimited to fixed length

Hi, I have to change a tab delimited file to a fixed length file. For text fields I need to left justify and NULL fill to the right and for number fields I need to right justify and zero fill to the left. If there are spaces between words in a text field I need to keep them as spaces. I am using korn shell and AIX. Here is a portion of a flat file that I am working with (First field is text and 12 characters long and second is a number 10 characters long) -
Bob Smith<<tab>>139.90
Kathy Reys<<tab>>-40.50

Here is the output that I need -
Bob Smith 0000139.90
Kathy Reys 0000-40.50
  #2 (permalink)  
Old 10-25-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
What an odd way to handle negative numbers...
Code:
#! /usr/bin/ksh
typeset -L13 name
typeset -R10 val2
IFS="<<tab>>"
exec < datafile
while read name val ; do
         val2="0000000000000000"$val
         echo "$name $val2"
done
exit 0
  #3 (permalink)  
Old 10-26-2005
nelson553011 nelson553011 is offline
Registered User
  
 

Join Date: Sep 2004
Location: Minnesota
Posts: 13
Got me further

Thanks for that help, that got me most of the way to where I needed. Do you know how to do the NULL padding after the strings instead of space padding? I thought I figured it out with a -Z on the typeset, but it doesn't seem to be working. Thanks again for the help.
  #4 (permalink)  
Old 10-26-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Sheesh, what a crazy format. ksh uses strings which are null terminated and this makes working with nulls virtually impossible. We can pad with a substitute character and then translate all occurences of the sunstitute character to nulls as the last operation.
Code:
#! /usr/bin/ksh
typeset -L13 name2
typeset -R10 val2
PAD="%"
NULLS="${PAD}${PAD}${PAD}${PAD}"
NULLS="${NULLS}${NULLS}${NULLS}${NULLS}"
IFS="<<tab>>"
exec < data
while read name val ; do
        val2="00000000000000"$val
        name2=${name}${NULLS}
        echo "$name2 $val2"
done | tr $PAD "\000"
exit 0
  #5 (permalink)  
Old 10-26-2005
nelson553011 nelson553011 is offline
Registered User
  
 

Join Date: Sep 2004
Location: Minnesota
Posts: 13
That's it!!!

Thanks for your help.
  #6 (permalink)  
Old 10-26-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -F\011 -nale'print $F[0].ljust(12,0.chr),$F[1].rjust(10,"0")' file
  #7 (permalink)  
Old 10-26-2005
nelson553011 nelson553011 is offline
Registered User
  
 

Join Date: Sep 2004
Location: Minnesota
Posts: 13
1 More Thing

I thought I had this figured out, but I'm running into another scenario that I need help with. Do you know how I can fix this if I have no data in a field, <<tab>><<tab>>? What is happening is that it's just going to the next field where there is data and using that, but I need it to use the empty field. Not sure if I explained that well, so here's an example -
Input file - (first field left justified NULL padded (10bytes), second field left justified NULL padded (1 byte), and third field left justified NULL padded (10bytes))
Bob Smith<<tab>><<tab>>Susan Smit
Kathy Reys<<tab>>A<<tab>>Ron Davis

Output file currently getting -
Bob Smith<<NULL>>Susan Smit
Kathy ReysARon Davis<<NULL>>

Needed output -
Bob Smith<<NULL>><<NULL>>Susan Smit
Kathy ReysARonDavis<<NULL>>

Hopefully that makes sense, if not I can try to explain more.
Thanks.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:11 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0