Sponsored Content
Full Discussion: Printf padded string
Top Forums Shell Programming and Scripting Printf padded string Post 302955235 by Don Cragun on Tuesday 15th of September 2015 03:28:57 PM
Old 09-15-2015
Here is the correction to the last paragraph I wrote in post #18...

My code looks for trailing digits to determine the numeric part of your input (allowing other digits to appear elsewhere in the prefix). When there aren't any trailing decimal digits (as with an input line containing "S2<space><space>"), you don't get what you wanted because:
Code:
	NUMBER = substr($0, match($0, /[[:digit:]]*$/))

successfully matches zero decimal digits at the end of the string and saves an empty string in the variable NUMBER and the call to match has the side effect of setting RSTART to the offset in $0 where the first of zero decimal digits was found (5 being the address of the null byte terminating the string) and setting RLENGTH to the number of decimal digits found at the end of the $0 (0 in this case). (If the ERE had been [[:digit:]]+$ which matches one or more decimal digits at the end of the string instead of [[:digit:]]*$ which matches zero of more decimal digits at the end of the string, different problems would arise. This code was designed to work under several assumption including:
Quote:
Each input string is an alphanumeric string ending in one or more decimal digits.
Code:
	PREFIX = substr($0, 1, PRELEN = (RSTART - 1))

sets PREFIX to the prefix (your alphabetic part, but this will use the longest string at the start of the line that does not end in a decimal digit). And it sets PRELEN to the length of that string (4 = 5 - 1 in this case).
Code:
	DIGITS = LEN - PRELEN

sets DIGITS to the length of the string you want (8) minus the length of PREFIX (PRELEN), which in this case is 4 (8 - 4).
Code:
	printf "%s%0*d\n", PREFIX, DIGITS, NUMBER

and here we print the PREFIX saved above and uses the same %04d format to print the decimal digits found at the end of your input. And, since there weren't any digits at the end of the string and the awk printf %d specifier treats an empty string as the numeric value zero, it prints the entire input string as the prefix followed by four zeros:
Code:
S2  0000

If I was writing production code, I would test that the input meets the stated assumptions and produce a diagnostic message for that input line instead of producing garbage output from garbage input. If anyone wants to turn sample code provided by the volunteers here at The UNIX & Linux Forums into production code, verifying that input meets the stated input requirements is part of that task. (Note that I stated the input and output assumptions used by my script in post #10 in this thread. When the input meets all of the input assumptions, it produces the desired output. If one or more of those assumptions are not met, I make no claims about the output produced by my sample code as a result.)
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 06:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy