awkwatch

Cmd with pipe, awk and quotes into watch


Following this : https://superuser.com/questions/276701/using-the-watch-command-with-an-argument-that-contains-quotes

I want to put following line into watch cmd.

wc -l myfile | awk '{print "Done ",$1," of 587320"}'

Following above instructions I tried:

watch "wc -l myfile | awk '{print '"'Done '"',\$1,'"' of 587320'"'}'"

But got

awk: cmd. line:1: {print Done ,\, of 587320}
awk: cmd. line:1:              ^ backslash not last character on line
awk: cmd. line:1: {print Done ,\, of 587320}
awk: cmd. line:1:              ^ syntax error

Expected output :

Done  57776 of 587320

I'm overwhelmed by the quotes.


Solution

  • wc -l myfile | awk '{print "Done ",$1," of 587320"}'
    

    You do not need wc -l as this can be done by GNU AWK itself, following way

    awk 'END{print "Done ", NR, " of 587320"}' myfile
    

    Explanation: END is executed after all files (in this case one file) is done, NR built-in variable holds The number of input records awk has processed since the beginning of the program’s execution

    I'm overwhelmed by the quotes.

    To avoid problem of this kind you might exploit -f option of GNU AWK, create file named counter.awk with following content

    END{print "Done ", NR, " of 587320"}
    

    and then you might get same result as above command doing

    awk -f counter.awk myfile
    

    which fit easily into watch command

    watch "awk -f counter.awk myfile"
    

    Note: I have tested above solution by running

    for i in $(seq 1 100);
    do
        echo $i >> myfile
        sleep 1
    done
    

    in other terminal and using watch version procps-ng 3.3.17 AND awk version GNU Awk 5.3.1