I'm struggling with this problem. I have this line:
mplayer *.* 2>/dev/null | grep Playing
which just launchs mplayer
and only the line matching Playing filename
is printed on screen.
This works also every time I change currently played media.
I was wondering if it was possible to "format" the output of grep. For example, if the output is:
Playing Pink_Floyd-Wish_You_Were_Here.mp3 # I get this at the beginning
Playing Pearl_Jam-Once.mp3 # I get this after I change media inside mplayer
then I would have something like:
Mplayer: Playing Pink_Floyd-Wish_You_Were_Here.mp3
Mplayer: Playing Pearl_Jam-Once.mp3
To do so, I decided to employ awk:
mplayer *.* 2>/dev/null | grep Playing | awk '{print "Mplayer: "$1}'
But in this case nothing happens; I get no output!!!
My question is not oriented to solve the problem with mplayer, but to have a deeper understanding about pipes in bash.
For example, if I use the following:
echo a_song.mp3 | awk '{print "Mplayer: "$1}'
I get what I want
Mplayer: a_song.mp3
Why does the first command not work?
What do I have to do if I want to run more than one process like this in background?
awk
command? What happens if you domplayer *.* 2>/dev/null | grep Playing > foo
and thenawk '{print "Mplayer: "$1}' < foo
? Oh, BTW:$1
means just the first word, so I would expect you to get "Mplayer: Playing". You probably want$0
.mplayer Playing* 2>/dev/null | awk '/Playing/{print "Playing: " $0}'
and you're donemplayer *.* 2>/dev/null | grep Playing
produces output to the terminal, then redirecting it to a file should get that same output. Now you know where the problem is, and so where to focus your investigation.mplayer *.* 2>/dev/null | awk '/Playing/ {print "Mplayer: " $0}'
(note that you have two errors in your suggestion) will fix this particular problem, it doesn’t explain why the command he tried produced no output.