Pad Zeros at the end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pad Zeros at the end
# 8  
Old 12-13-2011
Traditional way!
Code:
echo 12121 | awk '{printf $0;while(len-->0){printf char}}' len=5 char=9

HTH
--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 9  
Old 12-13-2011
if you want mathematical code then here it is:
Code:
$tmp=$ecode;
$count=0;

while($tmp>0)
{ $tmp=$tmp/10; $count++; }

$output=$ecode;
$character=9;

for($i=0;$i<$length-$count;$i++)
{         $output=$output*10+$character;      }

print $output;


Last edited by Franklin52; 12-13-2011 at 07:59 AM.. Reason: Please use code tags for code and data samples, thank you
# 10  
Old 12-13-2011
If your using bash you could setup some functions:
Code:
#!/bin/bash
function fill
{
  # fill string to width of count from chars 
  #
  # usage:
  # fill [-v var] count char
  #
  # if count is zero a blank string is output
 
  FILL="${2:- }"
  for ((c=0; c<=$1; c+=${#FILL}))
  do
    echo -n "${FILL:0:$1-$c}"
  done
}
 
function pad
{
  # Pad to right of string to required width, using chars.
  # Chars is repeated, as required, until width is reached.
  #
  # usage:
  # pad [-v var] width string <chars>
  #
  # if chars not specified spaces are used
 
  BACK=$(fill $1 "$3")
  let PAD=$1-${#2}
  if [ $PAD -lt 1 ] 
  then
    echo -n ${2:0:$1-1}
  else
    echo -n "$2${BACK:${#2}}"
  fi
}
 
echo $(pad 10 12121 0) $(pad 10 12121 9)
echo $(pad 10 12121 "-.")
echo $(pad 10 12 "-.")

Output:
Code:
1212100000 1212199999
12121.-.-.
12-.-.-.-.

# 11  
Old 12-14-2011
The Perl way.
Code:
$ echo "123" | perl -e 'chomp($x=<>);$pad_chr="0";$len=3; print $x . ($pad_chr x $len)'
123000

Sans redundance.
Code:
$ echo "123" | perl -e 'chomp($_=<>);print $_ .("0"x3)'
123000


Last edited by balajesuri; 12-14-2011 at 12:17 AM..
This User Gave Thanks to balajesuri For This Post:
# 12  
Old 12-14-2011
its grt ... Thx for all ur help...
it worked
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pad 0 to the right

I need to pad 0 to a number on the right. to make it 9 digit in total. My number is 2457 output should be 245700000 Please do wrap your samples/codes into CODE TAGS as per forum rules. (3 Replies)
Discussion started by: varun22486
3 Replies

2. Shell Programming and Scripting

How to pad with leading zeros for current time?

I'm using cygwin bash to submit scheduled tasks (kinda like cron jobs) in windows and the following script is giving me grief. I need to format the current time with leading zeros before 10AM for the hour field. In this example, I manually typed in "09:50" instead of using the `printf...`... (2 Replies)
Discussion started by: siegfried
2 Replies

3. Shell Programming and Scripting

Pad space at the end of string and reformat

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (11 Replies)
Discussion started by: menglm
11 Replies

4. Shell Programming and Scripting

Reformat a string and pad space at the end

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (1 Reply)
Discussion started by: menglm
1 Replies

5. Programming

How to right pad with zeros using sprintf?

I need to right-pad with zeros a string by using (s)printf. I looked up the manual and tried with printf("%-19s", buffer); which right-pad the string with spaces. So I tried printf("%019s", buffer); which left-pad the string with zeros. So I tried both printf("%-019s", buffer);... (9 Replies)
Discussion started by: emitrax
9 Replies

6. Shell Programming and Scripting

Pad zeros to a number

Pad zeros to a number and assign it to a variable like i get 1 in $i ,i want it to be $i as 01 (6 Replies)
Discussion started by: anumkoshy
6 Replies

7. UNIX for Dummies Questions & Answers

pad Zeros

Hi can I know command to pad Zeros to a value I get 16 and I need to send 0000000016 (5 Replies)
Discussion started by: mgirinath
5 Replies
Login or Register to Ask a Question