Results 1 to 10 of 17
Hi,
I was wondering if there was a command to zero pad a number on the left side?
Thanks...
- 03-16-2007 #1Just Joined!
- Join Date
- Jun 2006
- Posts
- 5
How to zero pad a number
Hi,
I was wondering if there was a command to zero pad a number on the left side?
Thanks
- 03-16-2007 #2Linux User
- Join Date
- Aug 2005
- Posts
- 408
I'm not sure I fully understand your question. Did you want to do this within a text file? If you do, you could probably use sed to do something like that.
Depending on the constraints involved, something like this:
Will turn this:Code:sed -e 's/^\([0-9]\)/0\1/g' samplefile
Into this:Code:893756982736 8972348596298762 09689273456982756 817984718942 1872412587165 n 32956928356 9u 98u6 u8459218204 009325661361 .103659161
This may not be exactly what you want, however.Code:0893756982736 08972348596298762 009689273456982756 0817984718942 01872412587165 n 32956928356 9u 98u6 u8459218204 0009325661361 .103659161
- 03-16-2007 #3Just Joined!
- Join Date
- Jun 2006
- Posts
- 5
I mean like
345
44
5435345
434
into
0000345
0000044
5435345
0000434
Is there some library that I could use for reference?
- 03-16-2007 #4
^^^ Those zeros should be ignored shouldn't they?
'Tis better to be silent and be thought a fool, than to speak and remove all doubt.'
--Abraham Lincoln
- 03-16-2007 #5Just Joined!
- Join Date
- Jun 2006
- Posts
- 5
I don't want them to be ignored. That's the purpose of zero padding.
Does anyone have any suggestions?
Thanks
- 03-16-2007 #6
Since I use Perl for just about everything, here's my solution in Perl:
Note that this code reads from standard input/files provided as arguments and spits out the result to standard output.Code:my $num_cols; # Is this fixed, or does it depend on the longest number? while(<>) { chomp; print '0' x ($num_cols - length); print; print "\n"; }Flies of a particular kind, i.e. time-flies, are fond of an arrow.
Registered Linux User #408794
- 03-16-2007 #7Linux User
- Join Date
- Oct 2004
- Location
- /dev/random
- Posts
- 404
You could simply use printf() (in both C and perl) for printing the output with leading zeroes.
For example,will print the number with leading zeroes and field width of 6 (min - if the no. has less digits than this width, it will be zero prepended).Code:printf("%06d", num)
HTH
The Unforgiven
Registered Linux User #358564
- 03-16-2007 #8
Gah! I thought there was a way to use printf, and there we go!
So the easiest way I can think of to do this would be:
This also reads from stdin (unless you provide files on the command line) and prints to stdout.Code:#!/usr/bin/perl use strict; chomp(my @lines = <>); my $longest_length; for $line (@lines) { $longest_length = length($line) if length($line) > $longest_length; } for $line (@lines) { printf('%0' . $longest_length - length($line) . "d\n", $line); }DISTRO=Arch
Registered Linux User #388732
- 03-16-2007 #9Just Joined!
- Join Date
- Jun 2006
- Posts
- 5
woops... I forgot to mention that I am doing this is shell script
It's supposed to be based on the length of the largest one (not fixed), but I can easily determine that already.
- 03-16-2007 #10Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Almost the same ... cheers, drl
SYNOPSIS
printf FORMAT [ARGUMENT]...
printf OPTION
DESCRIPTION
NOTE: your shell may have its own version of printf which will
supercede the version described here. Please refer to your shell's doc-
umentation for details about the options it supports.
Print ARGUMENT(s) according to FORMAT.
-- excerpt from man -S1 printfWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote