Add leading zero


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add leading zero
# 1  
Old 08-04-2008
Add leading zero

Hi there, I'm trying to add to a number leading zeros if it's just one digit. My code looks like this:
Code:
   d=`date +%d`
   if [ something ]; then 
      d=$((d-1)) # yesterday

      if (length($d) -eq 1); then
         d=0$d

      # this doesn't work if it's the 1st of the month :-(
   fi

...

$d can be instantiated with 04. After d=$((d-1)) $d is 3. Now I want to have it back to 03.
Any idea?
# 2  
Old 08-04-2008
echo $d | awk '{printf("%02d",$d)}'
# 3  
Old 08-04-2008
Thanks!

Actually I wanted to get the date from yesterday and I found this:

Code:
`TZ=CET+24 date +%y%m%d`

# 4  
Old 08-04-2008
well, if its a number, use math, doh !

you know, if its less than 10 (non inclusive ), add a 0

if [ $somevar -gt 10 ]
then
echo "0$somevar"
fi
# 5  
Old 08-04-2008
Using ksh93

Code:
$ printf "%(%y%m%d)T\n" yesterday
080803
$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Leading blanks

Hi Ich have a list as follows 73 5 100 45 81 4 and I would like to have an output (on screen) like that 73 5 100 45 81 4 (6 Replies)
Discussion started by: lazybaer
6 Replies

2. Shell Programming and Scripting

help in retaining leading zero

Hello. I'm trying to add multiple numbers with varying length and with leading zeroes in it. However, I'm getting the sum (totalHashAccountNumber) without the leading zeroes in it. How do I retain the leading zeroes? Please pardon the lengthy code.. I'm getting the hash account number from 2... (2 Replies)
Discussion started by: udelalv
2 Replies

3. UNIX for Dummies Questions & Answers

Add leading zeros to columns in a file

Hello Gurus, Quick question. I have a file with the following records: A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~ A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~ A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~ I wold like to do the following: 1. Add... (1 Reply)
Discussion started by: chumsky
1 Replies

4. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

5. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

6. Shell Programming and Scripting

Add leading zero's to the number in unix

Hi, Can anyone help me how to add leading zero's (0's) infront of numbers in unix script. For more details. x=450 Y=2344 i want the out put of 00000450, 000002344 (leading 5 zeros) Thanks A. Raj. (8 Replies)
Discussion started by: easterraj
8 Replies

7. Shell Programming and Scripting

Add leading zeroes to numbers in a file

Hello, I am (trying) to write a script that will check to see how many users are logged on to my machine, and if that number is more than 60 I need to kill off all the oldest sessions that are over 60. So far I have been able to check how many users are on and now I am at the part where I have to... (3 Replies)
Discussion started by: raidzero
3 Replies

8. UNIX for Dummies Questions & Answers

removing leading zero

Hi, I do have a code as follows: function deHex { if ]; then deHexDate=${1:1:$2} fi } The description given for the above code is as follows: # description: removes leading zero so value will not be recognized as hex # usage: deHex <value_parameter> <value_input_length> ... (0 Replies)
Discussion started by: risshanth
0 Replies

9. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies

10. Shell Programming and Scripting

Leading Zero's

I'm trying to write a script that compare's some numeric values in a file, but in one of the files the value always has leading zero's. For example - temp_8719_LM344_20031216091826: printera : 552 : 276 : 552 : 276 temp_8719_LM344_20031216091826: printera : 000552 :000276 : 000552 : 000276 ... (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question