Printf padded string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf padded string
# 1  
Old 09-11-2015
Printf padded string

Is possible to print padded string in printf?
Example
Code:
echo 1 | awk '{printf("%03d\n", $1)}'
001

I want
Code:
S1
S11
S2
S21

to be padded as:
Code:
S01
S11
S02
S21

Thanks!
# 2  
Old 09-11-2015
How about using substr function?
Code:
awk '{ printf("%s%02d\n",substr($0,1,1),substr($0,2)) }' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 09-11-2015
Thanks!
I should have thought of substr()!
Is there a simpler answer similar for number like printf "%04d" 12 #have 0012 to give any leading character you pick?
# 4  
Old 09-11-2015
Your request is a little vague. If you want leading X characters in a 5 character field when you are printing a number that is 1 to 5 digits:
Code:
awk '
BEGIN {	Xs = "XXXXX"}
{	printf("%.*s%d\n", 5 - length($1), Xs, $1)}' file

which with input:
Code:
1
234
56789

produces the output:
Code:
XXXX1
XX234
56789

Is this what you're trying to do?

As always, if you want to try this on a Solaris/SunOS System, change awk to /usr/xpg4/bin/awk or nawk.

You can do the same thing with a POSIX conforming shell (without needing to invoke awk) with:
Code:
printf '%.*s%d\n' $((5 - ${#1})) "XXXXX" "$1"

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-11-2015
I think he wanted more something like:

Code:
numbers="1 15 7 31 9"
digits=2
printf "S%0${digits}d\n" $numbers

Produces:
Code:
S01
S15
S07
S31
S09

hth

EDIT:
Just figured, Don's command is more dynamic.
But then again, one might want to start with digits=8 (for example) right on.

Last edited by sea; 09-11-2015 at 03:43 PM.. Reason: changed code
This User Gave Thanks to sea For This Post:
# 6  
Old 09-11-2015
Thanks!
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.
Yoda's answer is what I wanted, but I am wondering if there is a second way without stripping the leading char/string.
Another example:
Code:
input:
sk1
sk12
sk321
sk1344

Output:
Code:
sk0001
sk0012
sk0321
sk1344

Is that possible with printf?
# 7  
Old 09-11-2015
The printf utility (or awk printf function) is not able to determine where digits are in an alphanumeric string. Giving us continually different examples showing that our suggestions don't work when you change your input format is placing those of us trying to help you in a continuing game of whack-a-mole.

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.
This User Gave Thanks to Don Cragun For This Post:
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