Computing.Net > Forums > Unix > formatting unformatted data records

formatting unformatted data records

Reply to Message Icon

Original Message
Name: hernandez
Date: April 24, 2003 at 06:08:49 Pacific
Subject: formatting unformatted data records
OS: xp / korn shell
CPU/Ram: 196
Comment:

I have some records from a database. The database of course has all the field definitions and lengths so can display the data in a structured/easy to read way.

How can I write some script to allow to me display the data in a similarly defined structure within the korn shell.

My data looks like this unformatted (where there is a Z, there should be 8 blank spaces following it, but it is not showing up for some reason)

358974126254876270358974126254876245358974126254876213358974127254876240358974127254876206358974128Z 358974129254876279358974129254876263358974130254876191358974131Z 358974132254876278358974132254876234358974133254876250358974134254876277358974134254876152358974134254876142358974134254876132358974135254876256358974136254876205358974136254876156

And this is it formatted

358974126 254876270
358974126 254876245
358974126 254876213
358974127 254876240
358974127 254876206
358974128 Z
358974129 254876279
358974129 254876263
358974130 254876191
358974131 Z
358974132 254876278
358974132 254876234
358974133 254876250
358974134 254876277
358974134 254876152
358974134 254876142
358974134 254876132
358974135 254876256
358974136 254876205
358974136 254876156

Each field is 9 characters and each record contains 2 fields.

Thanks in advance for any help.


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 24, 2003 at 12:49:15 Pacific
Subject: formatting unformatted data records
Reply: (edit)

Hi:

The only way I see of doing this is to use the cut command to cut out the columns you want. The first thing I did was to set Z and 8 spaces. Your data only sets Z and 1.

If you have any questions or comments, let me know. (Don't forget the less than sign at the end of the script. This forum wipes it out)

Regards,


Nails

#!/bin/ksh

sed 's/Z/Z /g' dd.file > de.file


rlen=8
while IFS="&" txtline=$(line)
do
s=0
e=0
len=$(expr $txtline : '.*')
x1st=""
x2nd=""
while [ $e -lt $len ]
do
s=$((s+1))
e=$((s+rlen))

if [ -z $x1st ]; then
x1st=$(echo $txtline|cut -c$s-$e)
s=e # set the start to the end.
# beginning of the loop sets the next column
continue
fi

x2nd=$(echo $txtline|cut -c$s-$e)
printf "%s %s\n" $x1st $x2nd
s=e

x1st=""
x2nd=""
done
done de.file
# need an less than sign between done and de.file


Report Offensive Follow Up For Removal

Response Number 2
Name: hernandez
Date: May 3, 2003 at 09:26:52 Pacific
Subject: formatting unformatted data records
Reply: (edit)

forgot to look at this earlier
cheers that does the job


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: formatting unformatted data records

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software