Printf padded string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf padded string
# 15  
Old 09-14-2015
Thanks Don!
Your reply reminds me of the professor at my class years ago, who said only two subjects need be accurate/precise: one is law, the other is computer science(he meant programming, I think). Are you a lawyer as well?!
I did not anticipate such a long discussion!
Back to my original question. The input string is alphanumeric, with the alpha part leading and followed by numeric part of variable digits.
The output is padded so that the alpha part untouched, but the numeric part padded with leading "0" to have uniform length of digits according to the longest digits in the original alphanumeric string (as your 2nd reply did!).
Code:
S1 
S2 
S12 
S21 
sk1 
sk12 
sk321 
sk1344
strange1prefix2long99   # removed as very rare in my practice

Output
Code:
S0001 
S0002 
S0012 
S0021 
sk0001 
sk0012 
sk0321 
sk1344

Can we conclude the post now?
I do not want to get embarrassed more because of the inaccurate description of question, LOL!
Thanks again Don, and everyone!

Last edited by yifangt; 09-14-2015 at 04:00 PM..
# 16  
Old 09-14-2015
Computers resemble lawyers in that if given the slightest opportunity to misinterpret your instructions they probably will. You have to be precise, and learning to be that precise is half the battle.
This User Gave Thanks to Corona688 For This Post:
# 17  
Old 09-14-2015
I don't think you can do this in one operation. The % modifiers consider parts individually, not as one. How I'd do it is:

Code:
LEN=30 # How long you want the string to be
PRELEN="${#PREFIX}" # Length of string prefix
DIGITS=$((LEN - PRELEN))

printf "%s%0${DIGITS}d\n" $PREFIX $NUMBER

# 18  
Old 09-14-2015
I don't think you can do this in one operation.Then how to integrate this with a input file in simple shell script(eg, BASH), or awk, not thru compiling like C/C++?
Code:
test.file:
S1
S2  
S12  
S21  
sk1  
sk12  
sk321  
sk1344

Thanks!
# 19  
Old 09-14-2015
The code I gave you is shell script code.
# 20  
Old 09-14-2015
Thanks Corona688!
This what I tried by combining Don's reply.
Code:
awk '
NUMBER=substr($0, match($0, /[[:digit:]]*$/))
PREFIX=substr($0, 1, RSTART - 1)
LEN=8
PRELEN="${#PREFIX}"
DIGITS=$((LEN - PRELEN))
{printf "%s%0${DIGITS}d\n", $PREFIX, $NUMBER} ' < test.file

but did not work. What did I miss?
It seems to me Aia's perl oneliner is the simplest that I can understand better.

Last edited by yifangt; 09-14-2015 at 03:18 PM..
# 21  
Old 09-14-2015
$ means column in awk. awk does not have $ style variables or $(( )) style arithmetic. Any unquoted name is simply considered a variable.

Variables don't expand inside quotes in awk. To append them in strings you put them beside strings, ie. "prefix" var "suffix"

And all the code should be inside { } braces. The whole block will be run once per line.

Code:
$ awk '{
        A=$0 # Copy whole line into A
        sub(/^[^0-9]*/,"",A) # Delete prefix
        N=length($0)-length(A) # Measure length of prefix from this
        printf("%s%0" 8-N "d\n", substr($0,1,N), A); } ; ' input

S0000001
S0000002
S0000012
S0000021
sk000001
sk000012
sk000321
sk001344

$


Last edited by Corona688; 09-14-2015 at 04:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf or any other method to put long string of spec characters - passing passwords

Hello, I am looking for a method to use in my bash script which allows me to use long strings with all special characters. I have found that printf method could be helpful for me but unfortunately, when I trying root@machine:~# tevar=`printf "%s%c"... (2 Replies)
Discussion started by: elxa1
2 Replies

2. Shell Programming and Scripting

Unable to match string within awk printf

Hi All I am working to process txt file into csv commo separated. Input.txt 1,2,asdf,34sdsd,120,haahha2 2,2,wewedf,45sdsd,130,haahha ..... .... Errorcode.txt 120 130 140 myawk.awk code: { BEGIN{ HEADER="f1,f2,f3,f4,f5,f6" (4 Replies)
Discussion started by: krsnadasa
4 Replies

3. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf. If I try printing it using printf it is printing as follows. output ------- str1 str2 str3 str4 btw I'm working in AIX. This is my first post in this forum :) regards, rakesh (4 Replies)
Discussion started by: enigmatrix
4 Replies

5. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

6. Shell Programming and Scripting

Explanation for printf string in awk

hi all can any one help me to understand this bdf -t vfxs | awk '/\//{printf("%-30s%-10s%-10s%-10s%-5s%-10s\n",$1,$2,$3,$4,$5,$6)}' i want to understand the numbers %-30S% (4 Replies)
Discussion started by: maxim42
4 Replies

7. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

8. Shell Programming and Scripting

printf with Character String

I am trying to use printf with a character string that is used within a do loop. The problem is that while in the loop, the printf prints the variable name instead of the value. The do loop calls the variable name from a text file (called device.txt): while read device do cat $device.clean... (2 Replies)
Discussion started by: dleblanc67
2 Replies

9. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

10. Shell Programming and Scripting

find: problems escaping printf-command string

Hi Folks! Can you help me with this find -printf command. I seem to be unable to execute the printf-command from my shell script. I'm confused: :confused: My shell script snippet looks like this: #!/bin/sh .. COMMAND="find ./* -printf '%p %m %s %u %g \n'" echo "Command: ${COMMAND}"... (1 Reply)
Discussion started by: grahamb
1 Replies
Login or Register to Ask a Question