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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Masking fixed length file but variable number of characters 1

Status
Not open for further replies.

longs

Programmer
Jul 27, 2007
31
US
Hello,
I have a typical requirement.
I have a flat file which is a fixed length one.
In this flat file from 3rd position to 21st position (i.e 19 characters) is allocated for the acc #. The acc # may not be of full length. The length can vary from 10-19 characters.

Now my requirement is that I need to replace this acc # in such a way that leaving the first 6 digits and last 4 digits the remaining digits should be 0.

Eg.
123456789108738
8399456789911117386
5734567893333333

output
123456000008738
8399450000000007386
5734560000003333

As i posted something similar to this few days back at that time the requirement was different; sed work for me
Now as far as my knowledge goes first of all we need to find out the length of the acc #


Thanks to all for there help
 
Are the data fields in the file fixed? If the account number doesnt fill all 19 positions, are the unused positions blank?

IE

xx8399456789911117386xxx
xx839945678991111 xxx

If the data fields are not fixed, is there a delimiter that denotes the start/stop of each field?



Add a little color to your PUTTY terminal: faq52-6627
 
Its clumsy but...
Assuming we can extract just the first two chars and the A/C no then - using ksh (but not bash)
Code:
#!/bin/ksh
accno=$1
#find first eight chars
start=$(expr $accno : "\(........\).*")
#find last four chars
end=$(expr $accno : ".*\(....\)")
#find length
len=$(expr $accno : ".*")
#find lenght - 12 - i.e. lenght of middle bit
(( len -= 12 ))
#create string of $len zeros
typeset -Z $len mid=0
#print it all out
echo ${start}${mid}${end}
then
Code:
# ksh test.ksh ab123456789012345
ab123456000002345
# ksh test.ksh ab12345678901234567
ab12345600000004567
# ksh test.ksh ab1234567890
ab12345607890
Note the invalid result when the input was too short (2 prefix and 10 acc no chars)

Code:
 but I'm sure there's a better way to do it with awk.

Ceci n'est pas une signature
Columb Healy
 
I did some more research on typeset and you can use it to extract the start and end of the strings
So the MkII
Code:
#!/bin/ksh
accno=$1
#find first eight chars
typeset -L 8 start=$accno
#find last four chars
typeset -R 4 end=$accno
#find length
len=$(expr $accno : ".*")
#find lenght - 12 - i.e. lenght of middle bit
(( len -= 12 ))
#create string of $len zeros
typeset -Z $len mid=0
#print it all out
echo ${start}${mid}${end}
which is slightly faster than using expr.

Ceci n'est pas une signature
Columb Healy
 
Replying to sbrews question:

Yeh you are rite the data fields are fixed. if the account number doesn't fill all the 19 positions then you will have blanks.
Hope this clearify my problem in more detail


Are the data fields in the file fixed? If the account number doesnt fill all 19 positions, are the unused positions blank?
IE
xx8399456789911117386xxx
xx839945678991111 xxx
If the data fields are not fixed, is there a delimiter that denotes the start/stop of each field?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top