How do I filter stdout of a program through grep while still controlling the program?

10,031

Solution 1

You can give mpv a fake terminal by using the script(1) program, for instance:

script -c 'mpv FILE' /dev/null | grep -v 'Error while decoding frame'

For Mac OS X, the syntax seems to be:

script /dev/null mpv 'FILE' | grep -v 'Error while decoding frame'

[edit: also check out Trick an application into thinking its stdin is interactive for additional solutions]

Solution 2

You can use nohup mpv FILE. The output is written to nohup.out which you can filter using tail and grep:

nohup mpv FILE
tail -f nohup.out |grep -v 'Error while decoding frame'
Share:
10,031

Related videos on Youtube

jaxuru
Author by

jaxuru

Updated on September 18, 2022

Comments

  • jaxuru
    jaxuru over 1 year

    I'm trying to filter the output of the mpv media player, removing a particular line, but when I do so I am unable to control mpv with the keyboard. Here is the command:

    mpv FILE | grep -v 'Error while decoding frame'
    

    When I run the command, everything displays correctly, but I am unable to use the LEFT and RIGHT keys to scan through the file, or do anything else with the keyboard. How do I filter the output of the program while retaining control of it?

    • Anthon
      Anthon almost 9 years
      Are you sure mpv reads the keyboard if it detects it output is redirected to file? I.e if you do mpv FILE > somefile.out can you control it with the cursor?
    • jaxuru
      jaxuru almost 9 years
      @Anthon Oops, it doesn't, so would that imply that the place to look is mpv's settings?
    • Anthon
      Anthon almost 9 years
      Maybe there is some commandline switch for mpv. Quite a lot of programs actually detect if their output goes to a file (e.g. try man | grep -F a and you will not be able to scroll back and forth).
    • jaxuru
      jaxuru almost 9 years
      @Anthon there's a --input-terminal option but no dice, controls still changed, I think this is a dead end. Thanks for the help, though.
  • jaxuru
    jaxuru almost 9 years
    When I try to use script, it doesn't allow a -c option... and there's no --version option to try to see if its a version issue either. I'm running Mac OS Yosemite.
  • remram
    remram almost 9 years
    Yep, it's different; I added their syntax from the man page
  • Java Learner
    Java Learner about 4 years
    Tail just hangs waiting for text, even though mpv might have stopped producing output. If this is scripted, how do you detect EOF and exit?
  • Admin
    Admin almost 2 years
    Love that this way I don't have to stop the ongoing command in order to tweak the grep.