Wednesday, February 7, 2024

[SOLVED] How to run a program (a binary executable) inside awk?

Issue

I want to loop through rows in a text file and run ffmpeg with the field values as parameters. This is the code I tried:

awk -F, '{ffmpeg $1 -ss $2 -to $3 $4}' times.txt

and here's the content for times.txt

TheArtofSupporting.mkv,00:11:54,00:13:33,everyone is fighting a battle.mp4

But the output is empty. Probably I just assumed that awk can run a command inside, but if this is not possible what should I do to achieve my goal. Perhaps I can output the commands into a bash file, or pipe the output by print into sh ?


Solution

awk has a system function:

awk -F, '{system("ffmpeg "$1" -ss "$2" -to "$3" "$4)}' times.txt

This, like any thing that tries to construct a shell command from possibly untrusted input, should be considered fragile.



Answered By - chepner
Answer Checked By - Cary Denson (WPSolving Admin)