Sponsored Content
Top Forums Shell Programming and Scripting How to pad with leading zeros for current time? Post 302663735 by fpmurphy on Thursday 28th of June 2012 12:16:02 PM
Old 06-28-2012
You are correct, values starting with a leading zero (0) are interpreted as an octal. As the only values allowed in octal are {0..7}, an 8 or a 9 will cause the error message.

Workarounds include stripping the leading zero:
Code:
h="08"
h=`echo $h | sed 's/^0*//'`

or you can explicitly set the base when incrementing:
Code:
h="08"
h=$(( 10#$h + 1 ))

This User Gave Thanks to fpmurphy For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

how to retain leading zeros

Hi All, I am working with a fixed width file Forrmat. C1 Number (10,3) C2 Number (10,3) e.g. c1= 0000000100.000 c2= 0000000020.000 0000000100.0000000000020.000 I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting... (3 Replies)
Discussion started by: Manish Jha
3 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Help needed in padding leading zeros

Hi all, I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16. Example: cat tmp.txt 502455 50255 5026 5027 5028 Output 0000000000502455 0000000000050255 0000000000005026 0000000000005027 0000000000005028 Any... (12 Replies)
Discussion started by: jakSun8
12 Replies

6. 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

7. Shell Programming and Scripting

Pad Zeros at the end

I have number/strings like below input =23412133 output = 234121330000 (depends on the number give at runtime) i need to padd zeros based on runtime input . i tried below printf ' %d%04d\n', "23412133"; But the precision 4 is static here how can i pass this as runtime input. i am... (11 Replies)
Discussion started by: greenworld123
11 Replies

8. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

9. Shell Programming and Scripting

Help deleting leading zeros in a file

I have a list of numbers extracted and need to delete the leading zeros from them, but when i do so, the command I am using also deletes numbers that end in Zero as well. eg 10, 20, 30, etc this is part of a larger script and the only way I can think of is to try and detect the 10,20 30 etc in... (19 Replies)
Discussion started by: kcpoole
19 Replies

10. Shell Programming and Scripting

awk pad 1 column with leading zero if char > 12

Hello, I got a question. I have several csv files with lots of data in it and for the first column i have EAN codes. The problem that i am facing is that some of these codes have the leading 0 removed so they are 12 or less chars while a EAN code is (always?) 13 chars. For this i used a... (9 Replies)
Discussion started by: SDohmen
9 Replies
strptime(3C)															      strptime(3C)

NAME
strptime() - date and time conversion SYNOPSIS
DESCRIPTION
The function converts the character string pointed to by buf to values which are stored in the structure pointed to by tm, using the format specified by format. The format is composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters (as specified by the function); an ordinary character (neither nor a white-space character); or a conversion specification. Each conver- sion specification is composed of a character followed by a conversion character which specifies the replacement required. There must be whitespace or other non-alphanumeric characters between any two conversion specifications. The following conversion specifications are supported: is the day of week, using the locale's weekday names; either the abbreviated or full name may be specified. is the same as is the month, using the locale's month names; either the abbreviated or full name may be specified. is the same as is the date and time, using locale's date and time format (for example, as is the century number [0,99]; leading zeros are permitted but not required. is the day of month [1,31]; leading zeros are permitted but not required. is the date as is the same as is the same as is the hour (24-hour clock) [0,23]; leading zeros are permitted but not required. is the hour (12-hour clock) [1,12]; leading zeros are permitted but not required. is the day number of the year [1,366]; leading zeros are permitted but not required. is the month number [1,12]; leading zeros are permitted but not required. is the minute [0,59]; leading zeros are permitted but not required. is any whitespace. is the locale's equivalent of a.m or p.m. is the 12-hour clock time in AM/PM notation equivalent to format or the format specified in of the portion of the current locale, if it is not empty. Otherwise, returns NULL. is the time as is the seconds [0,60]; leading zeros are permitted but not required. is any whitespace. is the time as is the week number of the year (Sunday as the first day of the week) as a decimal number [0,53]; leading zeros are permitted but not required. All days in a year preceding the first Sunday are con- sidered to be in week 0. is the weekday as a decimal number [0,6], with 0 representing Sunday; leading zeros are permitted but not required. is the week number of the year (Monday as the first day of the week) as a decimal number [0,53]; leading zeros are permitted but not required. All days in a year preceding the first Monday are con- sidered to be in week 0. is the date, using the locale's date format. is the time, using the locale's time format. is the year within the century [0,99]; leading zeros are permitted but not required. If no century has been specified (for example, via the directive), the 20th century(1900s) is assumed for inputs in the range 69-99, and the 21st century(2000s) is assumed for inputs in the range 00-68. is the year, including the century (for example, 1992). is replaced by Modified Directives Some directives can be modified by the and modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified directive. If the alternative format or specification does not exist in the current locale, the behavior will be as if the unmodified directive were used. is the locale's alternative appropriate date and time representation. is the name of the base year (period) in the locale's alternative representation. is the locale's alternative date representation. is the locale's alternative time representation. is the offset from (year only) in the locale's alternative representation. is the full alternative year representation. is the day of the month using the locale's alternative numeric symbols; leading zeros are permitted by not required. is the same as is the hour (24-hour clock) using the locale's alternative numeric symbols. is the hour (12-hour clock) using the locale's alternative numeric symbols. is the month using the locale's alternative numeric symbols. is the minutes using the locale's alternative numeric symbols. is the seconds using the locale's alternative numeric symbols. is the week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols. is the number of the weekday (Sunday=0) using the locale's alternative numeric symbols. is the week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols. is the year (offset from in the locale's alternative representation and using the locale's alternative numeric symbols. A directive composed of whitespace characters is executed by scanning input up to the first character that is not whitespace (which remains unscanned), or until no more characters can be scanned. A directive that is an ordinary character is executed by scanning the next character from the buffer. If the character scanned from the buffer differs from the one comprising the directive, the directive fails, and the differing and subsequent characters remain unscanned. A series of directives composed of whitespace characters or any combination thereof is executed by scanning up to the first character that is not white space (which remains unscanned), or until no more characters can be scanned. Any other conversion specification is executed by scanning characters until a character matching the next directive is scanned, or until no more characters can be scanned. These characters, except the one matching the next directive, are then compared to the locale values asso- ciated with the conversion specifier. If a match is found, values for the appropriate structure members are set to values corresponding to the locale information. Case is ignored when matching items in buf such as month or weekday names. If no match is found, fails and no more characters are scanned. If the date specified exceeds the maximum time representable by the data type in 32-bit HP-UX (which repre- sents Tuesday January 19 03:14:07 UTC, 2038) or if the date exceeds the maximum date supported in 64-bit HP-UX (which is Friday December 31 23:59:59 UTC, 9999), fails and a null pointer is returned. EXTERNAL INFLUENCES
Environment Variables may define the alternative symbols (alt_digit; see localedef(4)) used by the modifier. The alt_digit definition has precedence over alt_digits (Support for alt_digit may be removed in a future release of HP-UX. determines the characters to be interpreted for those directives described above as being from the locale. determines the interpretation of the bytes within format as single- and/or multibyte characters. International Code Set Support Single- and multibyte character code sets are supported. RETURN VALUE
Upon successful completion, returns a pointer to the character following the last character parsed. Otherwise, a null pointer is returned. EXAMPLES
The following program segment uses to convert the string (first argument) to values according to the format specified in the second argu- ment. struct tm t; setlocale(LC_TIME, "en_US.iso88591"); strptime("1:04:23 PM on 10/6/92", "%I:%M:%S %p on %D", &t); The converted value is stored in the structure t as follows: t.tm_sec = 23 t.tm_min = 4 t.tm_hour = 13 t.tm_mday = 6 t.tm_mon = 9 t.tm_year = 92 t.tm_wday = 2 t.tm_yday = 279 t.tm_isdst = 1 AUTHOR
was developed by OSF and HP. SEE ALSO
scanf(3S), strftime(3C), getdate(3C), ctime(3C), setlocale(3C), thread_safety(5). STANDARDS CONFORMANCE
strptime(3C)
All times are GMT -4. The time now is 04:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy