Discussion:
awk, how to redirect to stderr
(too old to reply)
* Tong *
2007-01-14 18:57:39 UTC
Permalink
Hi,

Is there any way to redirect awk output to stderr?

From the man page I can see that awk only allow to redirect to file;
Moreover, I tried the following but failed:

$ awk 'BEGIN {print 3 > STDERR; }'
awk: fatal: expression for `>' redirection has null string value

Please help.
thanks

tong
--
Tong (remove underscore(s) to reply)
http://xpt.sf.net/techdocs/
http://xpt.sf.net/tools/
--
Posted via a free Usenet account from http://www.teranews.com
Klaus Alexander Seistrup
2007-01-14 20:02:32 UTC
Permalink
Post by * Tong *
$ awk 'BEGIN {print 3 > STDERR; }'
This works for me:

$ awk 'BEGIN {print 3 >"/dev/stderr";}'

but it might be a gnuism

$ awk --version | head -1
GNU Awk 3.1.4
$

Cheers,
--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
Kenny McCormack
2007-01-14 22:49:22 UTC
Permalink
Post by Klaus Alexander Seistrup
Post by * Tong *
$ awk 'BEGIN {print 3 > STDERR; }'
$ awk 'BEGIN {print 3 >"/dev/stderr";}'
but it might be a gnuism
It's not a GNU-ism, it's a system-ism.

It will work on any system that supports the /dev/stderr construct;
i.e., most modern Unixes. You could also try /dev/fd/2.

The AWK manual gives the "portable" (i.e., ugly) alternative of:

print | "cat >&2"

P.S. In TAWK, you can do: print > stderr
because "stderr" is a magically supplied value (like in C).
Stephane CHAZELAS
2007-01-14 23:02:29 UTC
Permalink
Post by Kenny McCormack
Post by Klaus Alexander Seistrup
Post by * Tong *
$ awk 'BEGIN {print 3 > STDERR; }'
$ awk 'BEGIN {print 3 >"/dev/stderr";}'
but it might be a gnuism
It's not a GNU-ism, it's a system-ism.
Not necessarily, some awks may consider "> /dev/stderr"
specially just like GNU bash on systems that don't have such
device files. I might be wrong, but I think gawk is one of
those. To be double checked.

See POSIX uuencode for standard tools that follow that
kind of convention (for /dev/stdout only though).
Post by Kenny McCormack
It will work on any system that supports the /dev/stderr construct;
i.e., most modern Unixes. You could also try /dev/fd/2.
I wouldn't say most. Anyone has figures?
Post by Kenny McCormack
print | "cat >&2"
P.S. In TAWK, you can do: print > stderr
because "stderr" is a magically supplied value (like in C).
--
Stéphane
Andre Majorel
2007-01-15 12:31:58 UTC
Permalink
Post by Stephane CHAZELAS
Post by Kenny McCormack
Post by Klaus Alexander Seistrup
$ awk 'BEGIN {print 3 >"/dev/stderr";}'
but it might be a gnuism
It's not a GNU-ism, it's a system-ism.
Not necessarily, some awks may consider "> /dev/stderr"
specially just like GNU bash on systems that don't have such
device files. I might be wrong, but I think gawk is one of
those. To be double checked.
I have vague memories of using ">/dev/stderr" on MS-DOS with GNU
awk 2 point something. That was a while ago, though, so don't
quote me on that.
--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
(Counterfeit: ***@nairobi.com ***@freestone.com)
First rule of Usenet : if we don't have an answer, it's not a
good question.
Ed Morton
2007-01-16 14:23:29 UTC
Permalink
Post by Kenny McCormack
Post by Klaus Alexander Seistrup
Post by * Tong *
$ awk 'BEGIN {print 3 > STDERR; }'
$ awk 'BEGIN {print 3 >"/dev/stderr";}'
but it might be a gnuism
It's not a GNU-ism, it's a system-ism.
It will work on any system that supports the /dev/stderr construct;
i.e., most modern Unixes. You could also try /dev/fd/2.
print | "cat >&2"
P.S. In TAWK, you can do: print > stderr
because "stderr" is a magically supplied value (like in C).
Obviously in any awk you can do this:

awk 'BEGIN{ stderr="cat >&2" }
...
{ print "ERROR: <scriptname>: foo" | stderr }'

though I prefer:

awk 'function prterr(txt) { printf "ERROR: <scriptname>: %s\n",txt |
"cat >&2" }
...
{ prterr("foo") }'

so you have one function for logging errors so it's easy to instrument
it later to do other things (e.g. prefix common error text as above,
dump variables, etc.) if you want.

Ed.

Loading...