I want to escape a DOS filename so I can use it with sed. I have a DOS batch file something like this:
set FILENAME=%~f1 sed 's/Some Pattern/%FILENAME%/' inputfile
(Note: %~f1 – expands %1 to a Fully qualified path name – C:\utils\MyFile.txt)
I found that the backslashes in %FILENAME% are just escaping the next letter.
How can I double them up so that they are escaped?
(I have cygwin installed so feel free to use any other *nix commands)
Solution
Combining Jeremy and Alexandru Nedelcu’s suggestions, and using | for the delimiter in the sed command I have
set FILENAME=%~f1 cygpath 's|Some Pattern|%FILENAME%|' >sedcmd.tmp sed -f sedcmd.tmp inputfile del /q sedcmd.tmp
This will work. It’s messy because in BAT files you can’t use set var=`cmd` like you can in unix. The fact that echo doesn’t understand quotes is also messy, and could lead to trouble if
Some Patterncontains shell meta characters.[Edited]: I am suprised that it didn’t work for you. I just tested it, and it worked on my machine. I am using sed from http://sourceforge.net/projects/unxutils and using cmd.exe to run those commands in a bat file.