Re: printf troubles
- From: Janis <janis_papanagnou@xxxxxxxxxxx>
- Date: Thu, 26 Jul 2007 02:11:06 -0700
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?
.
- References:
- printf troubles
- From: Greg Michael
- Re: printf troubles
- From: Janis Papanagnou
- Re: printf troubles
- From: Greg Michael
- printf troubles
- Prev by Date: Re: Newbee question about printf
- Next by Date: Re: Newbee question about printf
- Previous by thread: Re: printf troubles
- Next by thread: Newbee question about printf
- Index(es):
Relevant Pages
|