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

Padding zeros in front of qty field

Status
Not open for further replies.
May 23, 2002
8
0
0
US
I'm writing a script where I'm reading a file to extract needed information that will be used to crate another file that will be loaded into our M/F system. The file I'm reading has a qty associated with each record. When I'm creating my driver file for the M/F, they request that this qty field be padded with zeros. Here is some examples:

Source File Value Driver File Value needed

27 000027
134 000134
1005 001005

It has to have a length of 6 with padded zero's, is their an easy way to do this. I was looking at evaluating the length of the read qty, then figure my output based on that...
 
JT:

An easy way is to use awk's printf statement:

awk ' {
printf("%06d\n", $0)
} ' data.file

I'm assuming your data.file is 1 column with the required number.

Regards,

Ed
 
you could do it with perl as well

#!/usr/opt/perl5/bin/perl
eval 'exec /usr/opt/perl5/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches

while (<>) {
chomp; # strip record separator
if ($awk) {
printf &quot;%06d\n&quot;, $_;
}
print $_ if $data . . $file;
} --
| Mike Nixon
| Unix Admin
| ----------------------------
 
a korn shell solution:
typeset -RZ6 newvariable=${oldvariable}
This will right justify, zero fill, with length of 6
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top