how to add 0 in 1-9


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to add 0 in 1-9
# 1  
Old 01-14-2009
how to add 0 in 1-9

Any idea how can i have an output like this. please see below.

Desired output
test01
test02
.
.
test10

not
test1
test2
# 2  
Old 01-14-2009
Use printf with a "%02d" token to get leading zeroes.

Jerry
# 3  
Old 01-14-2009
Code:
pad()
{
  case $var in
    *[0-9][0-9]) ;;
    *[0-9]) temp=${var%?}; var=${temp}0${var#"$temp"} ;;
  esac
}

for var in test1 test2 k9 test10 test11
do
  pad
  printf "%s\n" "$var"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add add some files together and then paste

How can you efficiently add pairs of files together and then paste those pairs, I have a climate computer at work which spits out temperatures and stuff out twice a day, one for the day-data and one for the night (basically). Every week I want to make a nice overview The text files are... (1 Reply)
Discussion started by: uwgandalf
1 Replies
Login or Register to Ask a Question