Search code examples
windowscmdgrep

I need to printout -grep results to a file, How? (Windows OS)


I have a problem, which I'd previously solved, but I got a system error and lost the saved backups of my command lines.

I just want to print out the result of a command directly to a file after I get it parsed through a windows port of grep.

cmd /U /k ffmpeg -i "C:\video.webm" -af silencedetect=noise=-50dB:d=0.1 -f null - > "C:\!TEMP.txt" 2>&1 | -grep silencedetect "C:\!TEMP.txt" > "C:\!TEMP.txt" & type "C:\!TEMP.txt"

OR

cmd /U /k ffmpeg -i "C:\video.webm" -af silencedetect=noise=-50dB:d=0.1 -f null - | -grep silencedetect > "C:\!TEMP.txt" 2>&1 & type "C:\!TEMP.txt"

From what I recall, the command didn't work due to an issue whereby cmd itself printed the incomplete result from ffmpeg before being fully be parsed by grep. The solution to this, if I remember correctly, was to switch places of some pieces of the command line and shorten it a little. I'm almost sure that I didn't add anything else to that used in the sample below, (maybe stdout and &/&&, not sure).

On my attempts today it either didn't output, or gave me an error that the file is already in use.

Can someone please help me?

I don't want, and didn't need to use, more than one file. If I try to split it a little it looks silly.


Solution

  • Syntax that I used that needed a fix:

    cmd /U /k ffmpeg -i "C:\video.webm" -af silencedetect=noise=-50dB:d=0.1 -f null - | -grep silencedetect >"C:\!TEMP.txt" 2>&1 & type "C:\!TEMP.txt"
    

    the solution that I forgot and then asked for, which I said was silly:

    cmd /U /k ffmpeg -i "C:\video.webm" -af silencedetect=noise=-50dB:d=0.1 -f null - 2>&1 | -grep silencedetect >"C:\!TEMP.txt" 2>&1 & type "C:\!TEMP.txt"
    

    Then, just adding this "2>&1" after ffmpeg's arguments was enough, no matter the setup/output/etc, anyone could use it without further specificities.

    Well, now I'm facing just another issue that is more like a small nuisance, and maybe I'll be able to get an answer here without having to start another topic, for this:

    cmd /U /k ffmpeg -i "C:\video.webm" -af silencedetect=noise=-50dB:d=0.1 -f null - 2>&1 | FOR /F "tokens=8" %f in ('findstr "silence_duration:"') do @echo %f >>"C:\!TEMP.txt" 2>&1 & type "C:\TEMP.txt"
    

    As I did have to use Echo to be able to printout to the file(otherwise if nothing in place it doesn't printsout), I was also forced to use it as Append(>>),and not(>) that would be ideal for me since I don't want to mix different file outputs, Because while Echo is able to print to console with all its outputs when multiple requested strings were found with (>), printing to file this way just gets the last occurency of it; So, please if anyone reading this Actually knows an alternative to Echo on this situation Or a way to workaround on this syntax to be able to fully printout to a file through(>) what is? ...but please,if you don't know,don't answer/bloat on it, then I'll simply create another thread for this specific matter, thanks again in advance.