Results 1 to 10 of 10

Thread: Help with 'grep' and 'pipes'

  1. #1
    Join Date
    Jul 2007
    Location
    Dortmund, Germany
    Beans
    26
    Distro
    Kubuntu 7.04 Feisty Fawn

    Help with 'grep' and 'pipes'

    Hi there, I'm trying to make this command run, but I'm not getting the expected results:

    Code:
    $ mplayer test4.mp3 | grep -e "Title:" -e "Artist" > out.txt
    What happens is that the file 'out.txt' remains empty until the song finishes playing. Then it gets the expected information.

    Code:
    $ cat out.txt
     Title: Fee
     Artist: Phish
    But when I *not* redirect the output:

    Code:
    $ mplayer test4.mp3 | grep -e "Title:" -e "Artist"
    mplayer: could not connect to socket
    mplayer: No such file or directory
    Failed to open LIRC support. You will not be able to use your remote control.
     Title: Fee
     Artist: Phish
    AO: [pulse] Failed to connect to server: Connection refused
    The information is shown immediately, can someone help me out to figure out what am I doing wrong? How could I get the information on the file immediately ?

    Thanks !

  2. #2
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Help with 'grep' and 'pipes'

    The output of mplayer is piped to grep when mplayer exits.

    I would use mp3info
    Code:
    mp3info -p "Title:  %t\nArtist: %a\n" /path/to/file > file
    Last edited by sisco311; October 15th, 2009 at 10:01 PM.

  3. #3
    Join Date
    Jul 2007
    Location
    Dortmund, Germany
    Beans
    26
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Help with 'grep' and 'pipes'

    But why then, I can grep the output of mplayer when it is still playing the file ?

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Help with 'grep' and 'pipes'

    Quote Originally Posted by tosszyx View Post
    But why then, I can grep the output of mplayer when it is still playing the file ?
    yep, you are right.

    grep is writing to stdout (standard output),
    stdout is redirected to the file when grep exits.

  5. #5
    Join Date
    Dec 2006
    Beans
    7,349

    Re: Help with 'grep' and 'pipes'

    Hi tosszyx,

    Quote Originally Posted by tosszyx View Post
    Hi there, I'm trying to make this command run, but I'm not getting the expected results:

    Code:
    $ mplayer test4.mp3 | grep -e "Title:" -e "Artist" > out.txt
    What happens is that the file 'out.txt' remains empty until the song finishes playing. Then it gets the expected information.
    It is a little ugly but you could try:

    Code:
    mplayer test4.mp3 -ao null -endpos 1 | grep -e "Title:" -e "Artist" > out.txt
    and this works well enough on my system...

    Andrew
    You think that's air you're breathing now?

  6. #6
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Help with 'grep' and 'pipes'

    or:
    Code:
    mplayer file.mp3 | while read line; do echo $line | grep Title >> out.txt; done

  7. #7
    Join Date
    Sep 2006
    Beans
    1,610
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Help with 'grep' and 'pipes'

    Quote Originally Posted by sisco311 View Post
    yep, you are right.

    grep is writing to stdout (standard output),
    stdout is redirected to the file when grep exits.
    Almost.

    mplayer writes to stdout which is a buffered stream. If the ouput exceeds about 4KB, grep will catch some output. Otherhow, the output won't be physically written until the stream is flushed, which occurs as exit(2) closes the stdio streams for mplayer.

    Pipes work best when the stream source exits quickly and shoves its output through the pipeline (like mp3info, although it doesn't require any pipes to do what you want).

  8. #8
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Help with 'grep' and 'pipes'

    Quote Originally Posted by Giblet5 View Post
    Almost.

    mplayer writes to stdout which is a buffered stream. If the ouput exceeds about 4KB, grep will catch some output. Otherhow, the output won't be physically written until the stream is flushed, which occurs as exit(2) closes the stdio streams for mplayer.

    Pipes work best when the stream source exits quickly and shoves its output through the pipeline (like mp3info, although it doesn't require any pipes to do what you want).
    Thanks for the clarification!

  9. #9
    Join Date
    Jul 2007
    Location
    Dortmund, Germany
    Beans
    26
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Help with 'grep' and 'pipes'

    @andrew.46: Thanks a lot, unfortunately I'm trying to reencode some non-mp3 files to mp3 so I do need for the file to play, otherwise it was a nice hack !

    @sisco311: Great trick !! It works excellent. Thank you for your help.

    @Giblet5: Thanks a lot for sharing with us, it is very interesting.

  10. #10
    Join Date
    Jul 2007
    Location
    Dortmund, Germany
    Beans
    26
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: Help with 'grep' and 'pipes'

    Hi, what I'm trying to do is to reencode some files to mp3 trying to obtain the information from the tags information from mplayer (that I'm already using to decode the files).

    Right now I have:

    Code:
    mplayer test4.mp3 | while read line; do echo $line | grep -e "Title: " -e "Artist: " | awk -F ": " '{print $2}'; done
    And from that I get the output like:

    Code:
    mplayer: could not connect to socket
    mplayer: No such file or directory
    Failed to open LIRC support. You will not be able to use your remote control.
    AO: [pulse] Failed to connect to server: Connection refused
    Fee
    Phish
    But I can not find a way to assign that output inside a script to some variables, because all the piping. Any ideas?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •