Quote:
Originally Posted by
yifangt
Thanks Don!
I never meant to refuse to answer your question. Wish I did not offend you when I should have pointed my reply corresponding to your very question.
Originally I was trying to find the format specifier of printf() to pad any alphanumeric string in each row. Googled for a while, no luck. Yoda's first reply DID give the answer of my example, but not in the way I could think of. However, your first reply clarified that there is no such specifier for printf() to pad alphanumeric string.
Then I gave a second example to describe what I was looking for, which is padding numbers of the alphanumeric string with "0". The padding is only for the numeric part of the string.
Your "final attempt" did give more than what I wanted, as my case is not that complicated.
Did I answer your questions now?!
Thanks a lot!
I wasn't offended; just disappointed...
In post #1 in this thread, you said:
Quote:
Is possible to print padded string in printf?
and the four sample inputs you provided and the output you said you wanted seemed to indicate that you wanted two digit, leading zero filled numbers. And Yoda showed you how to do that.
In post #3, you said:
Quote:
Is there a simpler answer similar for number like printf "%04d" 12 #have 0012 to give any leading character you pick?
which I misread to mean that you wanted to use some character to pad numeric strings other than the <space> and <digit 0> fill that
printf format strings
%4d and
%04d provide. And, I showed you how you can use any character you want instead of space and zero.
So, in post #6 you said:
Quote:
The format of my input is a combination of string(or a char, in the example) plus number, and the numbers are with different length of digits.
and you gave four more sample input strings and showed us the output you wanted for those specific four input strings. And, the samples implied that you wanted four digit zero filled numbers this time. Note that there is nothing in the above description that says that the
string part of the input is alphabetic, fixed length, nor constant. Therefore, from the given descriptions to this point, there is no way to know where
string ends and
number begins for any input line, other than by making assumptions based on your two samples.
So, in post #7 in this thread, I said:
Quote:
Give us a clear definition of the input string formats you want to process, the output strings you want to produce from those input strings, and the parameters that will be supplied to specify output field width, fill characters to be used, where the input strings are coming from (a file, another string, command-line arguments, ...), how to determine where an input string prefix ends and the number begins, etc.
And, in post #8, you replied:
Quote:
I should be more specific with my examples. My input format always starts with char/string and followed by numbers of different length. So my original example starts with single char, which is ...
followed by the same two examples given earlier and repeating that the 1st example had a character that needed to be stripped and the 2nd example had a string (not two character string, not alphabetic string, not even fixed length string; just string) that needed to be stripped. You didn't supply any information that clearly defined the input string formats your script is supposed to handle, anything indicating if your script will receive a parameter indicating the width of the output string, anything indicating if your script will receive a parameter indicating how many digits should be displayed in the numbers, anything indicating if your script will receive a parameter indicating the desired fill character, and nothing indicating how to reliably determine the end of the initial "string" and the start of the "number" in your input.
So, I tried to come up with a set of requirements for a script that would read input in a format that seemed to cover all of your sample descriptions, write output based on characteristics of the input being processed, and produce output consistent with all of your sample descriptions. And then I wrote a script that tried to fulfill those requirements. It is relatively complicated because it has to deduce several parameters to be applied to the output by examining the input and making assumptions based on what it finds.
If the input
alpha string is a constant number of characters for a given invocation of your script, my script can be simplified. If the input
alpha string is the same string on every line for a given invocation of your script, my script can be simplified. If you tell the script how many digits you want in the
number portion of the output, my script can be simplified. But, you still haven't told use:
- if the alpha string is a constant number of characters for a given invocation of your script,
- if the input alpha string is the same string on every line for a given invocation of your script, nor
- how many digits you want in the number portion of the output.