Score:0

Redirecting output from printf to /dev/tty in awk

dk flag

I have a question on awk.

#!/usr/bin/nawk -f
{
    if (NF != 4) {
        error("Expected 4 fields");
    } else {
        print;
    }
}
function error ( message ) {
    if (FILENAME != "-") {
        printf("%s: ", FILENAME) > "/dev/tty";
    }
    printf("line # %d, %s, line: %s\n", NR, message, $0) >> "/dev/tty";
}

What does >"/dev/tty" and >>"/dev/tty" do?

It's from https://www.grymoire.com/Unix/Awk.html under User Defined Functions.

hr flag
It's for gawk rather than nawk, but see [5.7 Special Files for Standard Preopened Data Streams](https://www.gnu.org/software/gawk/manual/gawk.html#Special-FD)
Logan Lee avatar
dk flag
@steeldriver thx. i guess it is to redirect error message to stderr. the document you gave says it's better to do print "error!" >"/dev/stderr".
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.