combining sed with xargs to obtain a source and output file name

Posted by Lytithwyn on Super User See other posts from Super User or by Lytithwyn
Published on 2011-11-12T00:06:18Z Indexed on 2011/11/12 1:57 UTC
Read the original article Hit count: 527

Filed under:
|
|

I have a situation where I have some input files like this:

M2U0001.MPG
M2U0180.MPG

And I want to run a command (in a bash shell) on each similarly named file in the directory. I'd like the current file name to be given to this command as an input and a modified version of the filename to be given as an output file. Here's an example:

ffmpeg -i M2U0001.MPG M2U0001_fixed.MPG

I had the idea of using xargs and sed, but this is as far as I got:

ls -1 *.MPG | xargs -I{} ffmpeg -i {} `echo {} | sed -r 's/[0-9]{2,}/&_fixed/'`

But this results in the original filename being output in both positions. Am I totally going about this the wrong way?

I found that if I echo the filename directly to the embedded chunk like this it works:

echo M2U0001.MPG | sed -r 's/[0-9]{2,}/&_fixed/'

© Super User or respective owner

Related posts about bash

Related posts about sed