Home > Archive > AWK > February 2005 > how to truncate an output file
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 |
how to truncate an output file
|
|
| Kevin Rodgers 2005-02-09, 3:55 pm |
| I'm writing a program that splits the input stream into multiple output
files, whose names are determined at run time. At the point where the
program starts writing to a "new" output file, I want it to truncate the
file (in case it already exists). I thought this would do the trick,
but it initializes the file to 1 byte (the newline character):
{
outputfile = "something_that_depends_on_" $0
printf "" > outputfile
}
What's the right way to accomplish that?
Thanks,
--
Kevin Rodgers
| |
| John L 2005-02-09, 8:55 pm |
|
"Kevin Rodgers" <ihs_4664@yahoo.com> wrote in message news:36v50rF550ku1U1@individual.net...
> I'm writing a program that splits the input stream into multiple output
> files, whose names are determined at run time. At the point where the
> program starts writing to a "new" output file, I want it to truncate the
> file (in case it already exists). I thought this would do the trick,
> but it initializes the file to 1 byte (the newline character):
>
> {
> outputfile = "something_that_depends_on_" $0
> printf "" > outputfile
> }
>
> What's the right way to accomplish that?
>
Take no special measures, since the above does as you ask.
It may be clearer if instead of
printf "" > outputfile
you use
printf "first line of output here" > outputfile
--
John.
| |
| William James 2005-02-10, 3:56 am |
|
Kevin Rodgers wrote:
> I'm writing a program that splits the input stream into multiple
output
> files, whose names are determined at run time. At the point where
the
> program starts writing to a "new" output file, I want it to truncate
the
> file (in case it already exists).
When you write to the file, use > instead of >>.
Example. File "hi.txt" contains "Hi."
BEGIN {
print "foo" >"hi.txt"
print "bar" >"hi.txt" }
File "hi.txt" now contains "foo\nbar".
| |
| John DuBois 2005-02-12, 3:55 am |
| In article <36v50rF550ku1U1@individual.net>,
Kevin Rodgers <ihs_4664@yahoo.com> wrote:
>I'm writing a program that splits the input stream into multiple output
>files, whose names are determined at run time. At the point where the
>program starts writing to a "new" output file, I want it to truncate the
>file (in case it already exists). I thought this would do the trick,
>but it initializes the file to 1 byte (the newline character):
>
>{
> outputfile = "something_that_depends_on_" $0
> printf "" > outputfile
>}
Are you sure? In all of the awks I tested (gawk, mawk, sysv awk)
the above results in a 0-length file. Perhaps you actually used
print, not printf?
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
|
|
|
|
|