Zero fill a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Zero fill a string
# 1  
Old 02-22-2010
Zero fill a string

How do I zero fill a string. E.g pad a string to two digits

Code:
m=`date "+%m"`
pm=`expr $m - 1`
echo $pm

the above code echoes 1. I want it to echo 01. Any ideas?
# 2  
Old 02-22-2010
Code:
printf '%02d' $pm

# 3  
Old 02-22-2010
In Korn Shell
Code:
$ cat zerofill.ksh
#!/bin/ksh

m=`date "+%m"`
pm=`expr $m - 1`
echo $pm
typeset -Z2 ZERO_FILLED_NEXT_DAY=$pm
echo "ZERO_FILLED_NEXT_DAY=$ZERO_FILLED_NEXT_DAY"

exit 0

$ ./zerofill.ksh
1
ZERO_FILLED_NEXT_DAY=01

# 4  
Old 02-22-2010
Thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fill in missing values

Hi, I have a data sample as shown below. I want to fill in the left column so that the line will be continuous. For example, between 1 and 5 should be 2,3,4. And corresponding values in the right column will be 0. Thus the expected data should look like that: 1 1 1 10 1 2 1 3 1 5 1 6 2 0... (6 Replies)
Discussion started by: theanh0508
6 Replies

2. Linux

/var getting fill frequently

I have Red hat Linux server. There are 14 NFS shares which are coming from NAS. There are so many messages filling /var/log/messages, which seems like some NFS logging is enables somewhere, but I am not able to figure it out. Every few minutes, it will filll my /var (which is part of root) to 100%... (2 Replies)
Discussion started by: solaris_1977
2 Replies

3. Shell Programming and Scripting

Fill in missing Data

hello everyone, I have a task to input missing data into a file. example of my data below: Wed Feb 01 09:00:02 EST 2012,,,0.4,0.3,,0.3,,0.3,,0.5,,0.3,,,0.4,0.3, Wed Feb 01 09:00:11 EST 2012,,,,,,,0.2,,,,,,,,,, Wed Feb 01 09:00:22 EST... (23 Replies)
Discussion started by: Nolph
23 Replies

4. Shell Programming and Scripting

fill in last column of data

Hello, I am fairly new to awk, and I have the following problem. My file has missing data in the last column, and the program I am pre-processing this file for cannot interpret correctly shortened rows (it just wraps the data around). Is there a way to force awk to create the same... (6 Replies)
Discussion started by: timert34
6 Replies

5. Shell Programming and Scripting

Fill the directory

HI all When i do df -h,it displays the amount of free space. Tell me a command or a script to fill the total space to a certain percent (say 99%) Thanks in advance (8 Replies)
Discussion started by: 2002anand
8 Replies

6. UNIX for Dummies Questions & Answers

combine and fill series

hi all! i have a script with an output like this abc 123 1246 abc 1386 abc 2589 abc 7383 what can i do if i want to fill the 2nd column with the 2nd string '123' abc 123 1246 abc 123 1386 abc 123 2589 abc 123 7383 thanks! (3 Replies)
Discussion started by: engr.jay
3 Replies

7. Programming

Zero Fill 10 position field

Hi everyone, I am using shell scripting on a Unix SCO box. It calls a C program named zerofill. I do not know anything about C. I am having an issue with a hash total that should only be zero filled 10 positions but the record has 11 positions. DEBUG RESULTS + nice -n -5 bc -l /tmp/fhash8395... (0 Replies)
Discussion started by: ski
0 Replies

8. UNIX for Dummies Questions & Answers

Fill in columns

Hi, I've got a file with several columns where some entries are missing. Example: a b c d f g h j k l p y r I need to replace the empty spaces with zero, so the new file would be: a b c d f 0 g h j k 0 l p y 0 r The original file is tab delimited. Many thansk for... (3 Replies)
Discussion started by: zajtat
3 Replies

9. Shell Programming and Scripting

Fill the Key fields : Please help us

Hi .... we are having the below file .Column 1, Column 2 ,column 3 are key fields... In the below ...for 2 nd , 3 rd row the repeated key column is missing .... i want the new file to be populated with all missing key columns. ... (11 Replies)
Discussion started by: charandevu
11 Replies

10. Shell Programming and Scripting

Fixed length (Fill out)

hello, I have a file that has lines with different lengts. I want this file to be filled up with a zero so that al the lines have the same length. Please advice? File 1: ----------- 2310 2009 830 1345 Result File 2: ---------- 2310 2009 0830 (3 Replies)
Discussion started by: peterk
3 Replies
Login or Register to Ask a Question