Re: printf troubles



On 26 Jul., 05:20, Greg Michael <gmic...@xxxxxxxxxxx> wrote:
Janis Papanagnou wrote:
That's all a single format argument. What you actually have is

outfields = "a[1],a[2],a[3],a[4],a[5],a[6],a[7]"
format = "\"%-10s %-14s %10d %3s %02g %-5s %-1s %s\n\","
stmt = format outfields
printf (stmt)

where stmt is a single string. What you syntactically need is something
like

printf (stmt, a1, a2, a3, a4, a5, a6, a7, a8)

You assume some interpretation of the format string (a'la "eval") which
is not part of the awk language.

Your program shows quite some non-awk'ish "solutions"; maybe you want to
tell us what you want to achieve to help you with a more appropriate way
to do it.

Janis

This program is being used to monitor specific directories on a server
for changes to our VB apps. What it does is take a ls -lR output
captured in a flat file, and parses it for the specifically monitored
folders, and then formats the matching lines to remove unnecessary
spacing and characters. Indeed, this program is not meant to be
all-encompassing within awk, however, I know that awk has the ability to
clean up the contents of the file where Korn shell utilities alone
cannot (at least not easily).

If I read your response correctly, printf cannot accept a variable
substitution for the fields to be output? Correct?

You can have an arbitrary number of arguments (matching to the format
specification) but no dynamic extendable list (a'la va_arg, vprintf()
in the C language).

My records will
always have a minimum of 7 fields that are being output, but
occasionally, because of spaces in file names and folders, there are
more than 7 fields. I'm trying to find a way that I can dynamically
build a printf statement (for formatting purposes) that will always have
a minimum of 7 expressions, but can have more.

You can iterate over the remaining fields using a for loop and handle
them separately. You know that in awk the $ operator (as in $1 $2 $NF)
is an operator that can take arbitrary numeric values, also variables?
So var=8; print $var will print $8. That may be the key for a
solution, using var in a loop.

Janis

Here's a thought: what about using sprintf to output the statement into
a variable within my for loop, and once complete, print that variable?
Realistically, the problem I've come up against is how to output the
correct number of fields in the printf statement to match the number of
fields from the record?


.



Relevant Pages

  • Re: insert space between two words....
    ... In awk ... but, if you are trying to format the output, then printf() is probably the ... enough to pay for a netnews service....) ...
    (comp.lang.awk)
  • Re: insert space between two words....
    ... In awk ... but, if you are trying to format the output, then printf() is probably the ... That'll change all the other spacing in the line, ...
    (comp.lang.awk)
  • Re: ES format
    ... >> runtime format, even a runtime format that is trivially constant, not. ... You aren't allowed by Standard C to define any _external_ linkage ... In particular you certainly can define a 'static' function printf(), ... but otherwise it is part of the language and the compiler ...
    (comp.lang.fortran)
  • Re: PRI* considered harmful
    ... Now we're safe for every possible underlying type of any typedef name, ... the problem because of the different format specifications between double ... The _main_ argument about the insanity of printf(), ... standard functions, any better. ...
    (comp.std.c)
  • Re: vscanf and vprintf
    ... They take a pointer to a list of arguments rather than the arguments ... How can I write a function that takes a format string and a variable ... number of arguments, like printf, and passes them to printf to do most ... variable-length argument list, they accept a single va_list pointer. ...
    (comp.lang.c)