# how to use grep in a conditional search previous to a sed text replace command

I am trying to change the path in whole lot of m3u files. I know I can do it with regexxer f.e. but I'm trying to learn bash.

This is the command I figured out, but it is not working.

grep -lr -i -e 'M:\\' * | xargs -0 sed -i 's/M:\\/T:\\/g'

I suspect, but only suspect, it is due to special characters in filenames. How to deal with that?

-
First of all -0 option to xargs is wrong here, because it means the grep should send filenames separated by a NUL, which is not true (ok, it could be obtained by some grep option). Second, avoid using xargs in general, there are always better and safer ways to do the same thing. Lastly, it could help if you can show an input file, or at least some lines. –  enzotib Aug 31 '11 at 16:45

grep -lr -i -Z -e 'm:\\' * | xargs -0 sed -i 's/M:\\/K:\\/g'


I had to add the -Z to take care that every filename is read, and not the entire bunch in one long string.

-

This did the trick:

to change the windows path M:\ to the mount point in Ubuntu media/MODERNE MUZIEK

find -iname '*.m3u' -print0 |
xargs -0 sed -i 's/M:\\//\/media\/MODERNE\ MUZIEK\//g'


to change the the back slashes to forward slashes

find -iname '*.m3u' -print0 | xargs -0 sed -i 's/\\/\//g'


Now I can open my ex-windows m3u files with VLC player in Ubuntu.
It plays no matter how many spaces or weird characters are in the filename.

Could this be improved? could it be done with grep to make it conditional as in my first failed try? Why did grep not work?

Thanks for the input.

-