I am trying to print a particular text using awk if string is empty. It works fine in the below case
noob@noob:~$ echo "" | awk '{if ($0=="") print "not playing"}'
not playing
but when I try to take a similar approach in the below case it didn’t work
noob@noob:~$ mpc current | awk '{if ($0=="") print "not playing"}'
noob@noob:~$
I believe the output of mpc current if no song is playing is an empty string.
noob@noob:~$ mpc current
noob@noob:~$ #empty string
So, is my assumption of empty string wrong?
Apparently it prints nothing at all, not even a newline. Try this instead.
This prints any output. If you don’t want that, take out the
{ print }.awk processes each line in turn, and then at EOF performs any END block. If there were no input lines, there will be an EOF right at the start, the variable
NRwill be zero, and so the END block will print the placeholder text. (I originally had a dedicated variable for this, but the built-in line number variableNR, as used in Bob Vale’s answer, is decidedly more elegant. It is incremented for each input line thatawkreads.)