Home > Archive > AWK > November 2004 > PRINTING LOTS OF OUTPUT
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
PRINTING LOTS OF OUTPUT
|
|
| James Pascoe 2004-11-16, 6:50 pm |
| Dear All,
Apologies if this is OT or answered elsewhere, but I am looking for
some help.
I am using gawk to generate a C file as part of a build process, and
as such, the gawk script
does a lot of printing. Currently, I am using lots of print statements
(1 per line) to actually output the text. The body of my script looks
similar too:
print "int main(void) "
print "{"
....
print " return (0);"
print "}"
.... and basically, I was wondering if there is a better way to produce
lots of static text ?
Many thanks,
James
| |
| Kenny McCormack 2004-11-16, 6:50 pm |
| In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
James Pascoe <james@james-pascoe.com> wrote:
>Dear All,
>
>Apologies if this is OT or answered elsewhere, but I am looking for
>some help.
>
>I am using gawk to generate a C file as part of a build process, and
>as such, the gawk script
>does a lot of printing. Currently, I am using lots of print statements
>(1 per line) to actually output the text. The body of my script looks
>similar too:
>
>print "int main(void) "
>print "{"
>...
>print " return (0);"
>print "}"
>
>... and basically, I was wondering if there is a better way to produce
>lots of static text ?
The short answer is "not really". There's nothing really wrong with the
above, but sometimes I bunch them together, like this:
print "int main(void)\n{\n return (0);\n}"
at some cost in readability. Or, you could try:
OFS="\n"
print "int main(void)",
"{",
" return (0);",
"}"
| |
| Ed Morton 2004-11-16, 6:50 pm |
|
Kenny McCormack wrote:
> In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
> James Pascoe <james@james-pascoe.com> wrote:
>
>
>
> The short answer is "not really". There's nothing really wrong with the
> above, but sometimes I bunch them together, like this:
>
> print "int main(void)\n{\n return (0);\n}"
>
> at some cost in readability. Or, you could try:
>
> OFS="\n"
> print "int main(void)",
> "{",
> " return (0);",
> "}"
>
For that example, just set it up in one or more variables outside the
script, then print those, e.g.:
echo "" | gawk '{printf "%s%s",start,end}' start="void
main(void)
{
" end=" return 0;
}
"
You can escape the newlines between each variable definition if you
prefer for readability, e.g.:
echo "" | gawk '{printf "%s%s",start,end }' \
start="\
void
main(void)
{
" \
end="
return 0;
}
"
If you have other cases where the output gets built dynamically during
the execution of the program, we'd need to see examples to offer advice,
but the above works fine for static output.
By the way, no big deal but "return" is a language builtin in C, not a
function, so it's better style to not surround it in unnecessary brackets.
Regards,
Ed.
| |
| Janis Papanagnou 2004-11-16, 6:50 pm |
| James Pascoe wrote:
> Dear All,
>
> Apologies if this is OT or answered elsewhere, but I am looking for
> some help.
>
> I am using gawk to generate a C file as part of a build process, and
> as such, the gawk script
> does a lot of printing. Currently, I am using lots of print statements
> (1 per line) to actually output the text. The body of my script looks
> similar too:
>
> print "int main(void) "
> print "{"
> ...
> print " return (0);"
> print "}"
>
> ... and basically, I was wondering if there is a better way to produce
> lots of static text ?
My answer may be OT, too :-) But could it be that you are using an
inappropriate tool? If the main task is to create C templates, even
parameterized templates, you might want to have a look at the "here
document" feature of bourne-like shells.
cat <<- EOT
int main(void)
{
...
return 0;
}
EOT
Janis
| |
| Ed Morton 2004-11-17, 8:55 pm |
|
Kenny McCormack wrote:
> In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
> James Pascoe <james@james-pascoe.com> wrote:
>
>
>
> The short answer is "not really". There's nothing really wrong with the
> above, but sometimes I bunch them together, like this:
>
> print "int main(void)\n{\n return (0);\n}"
>
> at some cost in readability. Or, you could try:
>
> OFS="\n"
> print "int main(void)",
> "{",
> " return (0);",
> "}"
>
For that example, just set it up in one or more variables outside the
script, then print those, e.g.:
echo "" | gawk '{printf "%s%s",start,end}' start="void
main(void)
{
" end=" return 0;
}
"
You can escape the newlines between each variable definition if you
prefer for readability, e.g.:
echo "" | gawk '{printf "%s%s",start,end }' \
start="\
void
main(void)
{
" \
end="
return 0;
}
"
If you have other cases where the output gets built dynamically during
the execution of the program, we'd need to see examples to offer advice,
but the above works fine for static output.
By the way, no big deal but "return" is a language builtin in C, not a
function, so it's better style to not surround it in unnecessary brackets.
Regards,
Ed.
| |
| Kenny McCormack 2004-11-18, 8:56 pm |
| In article <fe6eb44f.0411150757.5f62fd22@posting.google.com>,
James Pascoe <james@james-pascoe.com> wrote:
>Dear All,
>
>Apologies if this is OT or answered elsewhere, but I am looking for
>some help.
>
>I am using gawk to generate a C file as part of a build process, and
>as such, the gawk script
>does a lot of printing. Currently, I am using lots of print statements
>(1 per line) to actually output the text. The body of my script looks
>similar too:
>
>print "int main(void) "
>print "{"
>...
>print " return (0);"
>print "}"
>
>... and basically, I was wondering if there is a better way to produce
>lots of static text ?
The short answer is "not really". There's nothing really wrong with the
above, but sometimes I bunch them together, like this:
print "int main(void)\n{\n return (0);\n}"
at some cost in readability. Or, you could try:
OFS="\n"
print "int main(void)",
"{",
" return (0);",
"}"
| |
| Janis Papanagnou 2004-11-21, 3:57 am |
| James Pascoe wrote:
> Dear All,
>
> Apologies if this is OT or answered elsewhere, but I am looking for
> some help.
>
> I am using gawk to generate a C file as part of a build process, and
> as such, the gawk script
> does a lot of printing. Currently, I am using lots of print statements
> (1 per line) to actually output the text. The body of my script looks
> similar too:
>
> print "int main(void) "
> print "{"
> ...
> print " return (0);"
> print "}"
>
> ... and basically, I was wondering if there is a better way to produce
> lots of static text ?
My answer may be OT, too :-) But could it be that you are using an
inappropriate tool? If the main task is to create C templates, even
parameterized templates, you might want to have a look at the "here
document" feature of bourne-like shells.
cat <<- EOT
int main(void)
{
...
return 0;
}
EOT
Janis
|
|
|
|
|