Operating System - HP-UX
1819818 Members
3289 Online
109607 Solutions
New Discussion юеВ

Can awk print to the standard error ?

 
SOLVED
Go to solution
Jdamian
Respected Contributor

Can awk print to the standard error ?

How can awk print to the standard error instead of standard output ?
9 REPLIES 9
Leif Halvarsson_2
Honored Contributor

Re: Can awk print to the standard error ?

awk {print} 1>&2
Arturo Perez del Galleg
Frequent Advisor

Re: Can awk print to the standard error ?

Are you tried:
awk '...' file 2>file2 1>&2
The standard error to a file and the redirection of the standard output to the same file.
But... what is the sense?
awk '...' file 1>file2 2>&1
obtains the same output.
HTH
Armin Feller
Honored Contributor

Re: Can awk print to the standard error ?

Hi,

you can use file descriptor 0 for standard input, 1 for standard output, and 2 for standard error:

# awk {print} 1>&2

Regards ...
Armin
Jdamian
Respected Contributor

Re: Can awk print to the standard error ?

Arturo, please, I don't want to collect the "awk" error messages... I want my awk program prints in standard error. For example:

awk ' { if (n==0) print "ERROR" }'

I want ERROR be printed in standard error, not in standard output.

Leif, your suggestion will print on standard error ANY message... I don't want that. I want the error messages be printed in standard error but I also want normal output be printed in standard output.
Leif Halvarsson_2
Honored Contributor
Solution

Re: Can awk print to the standard error ?

Hi,
Perhaps not very elegant but berhaps you can try it.

awk '{print | "cat 1>&2" }
Arturo Perez del Galleg
Frequent Advisor

Re: Can awk print to the standard error ?

Oops!
I'm surprised, this is your answer? In your first post you're saying this?
My recomendation is:
awk '...' file >file2
grep 'ERROR' file2 >file_error
The selection of the standard output that you want isn't possible.
By the way, is very curious that the all replies beeing similars the points not...
Jdamian
Respected Contributor

Re: Can awk print to the standard error ?

Thanx Leif.

This is what I needed.
harry d brown jr
Honored Contributor

Re: Can awk print to the standard error ?

J. Damian Benavent,

You can now go through your profile and find your previous posts and assign points (166 of 227): http://forums.itrc.hp.com/cm/TopSolutions/1,,BR158676!1!questions,00.html


live free or die
harry
Live Free or Die
Bob_Vance
Esteemed Contributor

Re: Can awk print to the standard error ?

FWIW,
'gawk' (gnu awk -- the 'awk' on Linux) supports the following syntax:

awk '{print "error" >> "/dev/stderr" } '

(note the special file name
"/dev/stderr"
)
as in:

# echo | awk '{print "hello"} ; print "error" > "/dev/stderr" } '

hello
error

# echo | awk '{print "hello"} ; print "error" > "/dev/stderr" } ' >/dev/null

error


bv
"The lyf so short, the craft so long to lerne." - Chaucer