Thanks Don! As a learning exercise, can you explain why the awk script Corona688 suggested didn't have a problem with trailing spaces while my script above does have a problem with trailing spaces?Not quite sure, but my guess is:
In Crona688's reply, sub(/^[^0-9]*/,"",A)removed all the trailing spaces, but yours did not substr($0, match($0, /[[:digit:]]*$/)) Correct?
Do the printf format modifiers make any difference?
Yours is
vs
This is my original un-clear that I want clarify.
No.
deletes everything from the start of the string that is not a decimal digit.
then computes the length of the alphabetic part of your input as the original line length minus the line length of the input with the characters that are not decimal digits at the start of your input removed. With the input line "S2<space><space>", N is set to 1, i.e., 4 (4 input characters) - 3 (the length of "2<space><space>" after removing the leading "S").
and this works because the substr() extracts the 1st character from the original input and prints it using the format %s and prints "2<space><space>" using the format %07d which prints the 7 (or more) digit leading zero filled number specified by A (and the %d format specifier ignores anything in the string it evaluates starting with the 1st character that is not part of a valid numeric value).
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:
saves the trailing decimal digits in the variable NUMBER and the call to match has the side effect of setting RSTART to the offset in $0 where the first decimal digit was found (zero if not match was found) and setting RLENGTH to the number of decimal digits found at the end of the $0 (-1 if no match is found).
sets PREFIX to the prefix (your alphabetic part, but this will take 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.
sets DIGITS to the length of the string you want (8) minus the length of PREFIX (PRELEN).
and here we print the PREFIX saved above and uses the same %0xd format to print the decimal digits found at the end of your input.
And, with the input "S2<space><space>", what we find if we look closely is that the awk match() function on Mac OS X (from BSD) does not conform to the standards. When no match is found RSTART should be set to zero, but instead it is being set to the length of the input string plus one. So, the almost reasonable output I showed you in post #22 for the input with trailing spaces is not what a standards-conforming awk should do. (I just love it when I find conformance bugs in UNIX implementations when I'm trying to explain how things should work! ) So, now I need to check to see what other implementations do to determine if this is a bug in the standards or a bug in BSD/Apple awk. Are we having fun yet...
Update: Please ignore the grayed out paragraph above... I obviously hadn't had enough sleep when I wrote it. I'll post an update later today explaining correctly how Apple/BSD awk is doing exactly what it is supposed to be doing with the input string "S2<space><space>"... I apologize for any confusion this may have caused.
Last edited by Don Cragun; 09-15-2015 at 03:09 PM..
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)