4

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?

4
  • whats the result of mpc current | awk '{print "X$0X"}' Commented Jun 27, 2012 at 8:29
  • It prints nothing. It's output is same as mpc current I mentioned above. Commented Jun 27, 2012 at 8:31
  • 2
    awk processes 'lines'. If there is no output from mpc current, there is no line to process. Commented Jun 27, 2012 at 8:38
  • @Noob It was just to confirm that the output of mpc current was not " ", (ie a single space) Commented Jun 27, 2012 at 8:42

3 Answers 3

14

Apparently it prints nothing at all, not even a newline. Try this instead.

mpc current | awk '{ print } END { if (!NR) print "not playing" }'

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 NR will 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 variable NR, as used in Bob Vale's answer, is decidedly more elegant. It is incremented for each input line that awk reads.)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, it worked perfectly. Can you explain it to me what you did here.
Added an explanation, as suggested in an edit by @Karl Nordström.
4

try

mpc current | awk 'END { if (NR==0) print "not playing"}'

1 Comment

you can add {print} before the END if you want to output the result
3

Is this really a job for awk? Looks to me like the simple matter of line counting:

mpc current | if [[ $(wc -L) -eq 0 ]]; then echo "not playing"; fi

The above code counts the length of the longest line output by mpc current, and prints "not playing" if it is 0.

Here is how it behaves:

$ echo ""       | if [[ $(wc -L) -eq 0 ]]; then echo "not playing"; fi
not playing
$

$ echo -n ""    | if [[ $(wc -L) -eq 0 ]]; then echo "not playing"; fi
not playing
$

$ echo "foo"    | if [[ $(wc -L) -eq 0 ]]; then echo "not playing"; fi
$

$ echo -n "foo" | if [[ $(wc -L) -eq 0 ]]; then echo "not playing"; fi
$

OR

As William Pursell says in their comment, you can also do:

mpc current | grep -q . || echo "not playing"

This doesn't require bash either.

5 Comments

of course this doesn't work if the OP wants it to print foo if the input is foo
@BobVale - The original snippet posted by the OP doesn't reproduce input. I assumed this is the required behavior. Plus, when writing a script, I would prefer a dependency on GNU coreutils over a dependency on GNU awk any day.
But why accept the bash dependency? You can do this much more simply (and without requiring bash): mpc current | grep -q . || echo "not playing" Notice that this makes it easy to satisfy Bob's criticism, since you can omit -q.
@WilliamPursell - +1. That should be an answer. I'd appreciate if you write one. If not, I'll go ahead and edit it in. As to why accept a bash dependency?, I never thought about it. It's the only shell I use. You are right of course.
grep . doesn't count empty lines. If this makes a difference to you, try grep '^' instead. The grep approach is clearly better than the antipattern of counting the number of output lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.