in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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.)

Related questions

0 votes
    I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: ... how to do this? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Hi I want to remove last comma from a line. For example: Input: This,is,a,test Desired Output: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
0 votes
    import pandas as pd from itertools import combinations, product, permutations MH_P= ["Maria Herrera"] OP_P= ["Oscar ... i solve it? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    How will you print count of empty strings in Java 8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    fairly new to using linux on shell. I want to reduce the amount of pipes I used to extract the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I am a newbie to Scala and I want to learn how can I add null and empty check on an optional ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    I have a table which contains 'NULL' values which are of type 'Datetime'. Now i have to convert ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    PHP's explode function returns an array of strings split on some provided substring. It will return empty ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    PHP's explode function returns an array of strings split on some provided substring. It will return empty ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
...