DOS文件名转义用于* nix命令

时间:2022-04-06 00:38:23

I want to escape a DOS filename so I can use it with sed. I have a DOS batch file something like this:

我想要转义DOS文件名,以便我可以使用它与sed。我有一个DOS批处理文件,如下所示:

set FILENAME=%~f1

sed 's/Some Pattern/%FILENAME%/' inputfile

(Note: %~f1 - expands %1 to a Fully qualified path name - C:\utils\MyFile.txt)

(注意:%~f1 - 将%1扩展为完全限定的路径名​​ - C:\ utils \ MyFile.txt)

I found that the backslashes in %FILENAME% are just escaping the next letter.

我发现%FILENAME%中的反斜杠只是转义下一个字母。

How can I double them up so that they are escaped?

我怎样才能使它们翻倍以便它们被逃脱?

(I have cygwin installed so feel free to use any other *nix commands)

(我安装了cygwin,所以随意使用任何其他* nix命令)

Solution

Combining Jeremy and Alexandru Nedelcu's suggestions, and using | for the delimiter in the sed command I have

结合Jeremy和Alexandru Nedelcu的建议,并使用|我有sed命令中的分隔符

set FILENAME=%~f1
cygpath "s|Some Pattern|%FILENAME%|" >sedcmd.tmp
sed -f sedcmd.tmp inputfile
del /q sedcmd.tmp

3 个解决方案

#1


2  

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 Pattern contains shell meta characters.

这会奏效。这很麻烦,因为在BAT文件中你不能像unix那样使用set var =`cmd`。 echo不理解引号的事实也很混乱,如果Some Pattern包含shell元字符,可能会导致麻烦。

set FILENAME=%~f1
echo s/Some Pattern/%FILENAME%/ | sed -e "s/\\/\\\\/g" >sedcmd.tmp
sed -f sedcmd.tmp inputfile
del /q sedcmd.tmp

[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.

[编辑]:我很惊讶它不适合你。我只是测试了它,它在我的机器上工作。我使用http://sourceforge.net/projects/unxutils中的sed并使用cmd.exe在bat文件中运行这些命令。

#2


2  

You could try as alternative (from the command prompt) ...

您可以尝试替代(从命令提示符)...

> cygpath -m c:\some\path
c:/some/path

As you can guess, it converts backslashes to slashes.

你可以猜到,它将反斜杠转换为斜杠。

#3


0  

@Alexandru & Jeremy, Thanks for your help. You both get upvotes

@Alexandru&Jeremy,谢谢你的帮助。你们都得到了赞成

@Jeremy

Using your method I got the following error:

使用您的方法我收到以下错误:

sed: -e expression #1, char 8: unterminated `s' command

sed:-e expression#1,char 8:unterminated`s'命令

If you can edit your answer to make it work I'd accept it. (pasting my solution doesn't count)

如果您可以编辑您的答案以使其正常工作,我会接受它。 (粘贴我的解决方案不算)

Update: Ok, I tried it with UnixUtils and it worked. (For reference, the UnixUtils I downloaded was dated March 1, 2007, and uses GNU sed version 3.02, my Cygwin install has GNU sed version 4.1.5)

更新:好的,我尝试使用UnixUtils并且它有效。 (作为参考,我下载的UnixUtils日期为2007年3月1日,使用GNU sed版本3.02,我的Cygwin安装有GNU sed版本4.1.5)

#1


2  

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 Pattern contains shell meta characters.

这会奏效。这很麻烦,因为在BAT文件中你不能像unix那样使用set var =`cmd`。 echo不理解引号的事实也很混乱,如果Some Pattern包含shell元字符,可能会导致麻烦。

set FILENAME=%~f1
echo s/Some Pattern/%FILENAME%/ | sed -e "s/\\/\\\\/g" >sedcmd.tmp
sed -f sedcmd.tmp inputfile
del /q sedcmd.tmp

[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.

[编辑]:我很惊讶它不适合你。我只是测试了它,它在我的机器上工作。我使用http://sourceforge.net/projects/unxutils中的sed并使用cmd.exe在bat文件中运行这些命令。

#2


2  

You could try as alternative (from the command prompt) ...

您可以尝试替代(从命令提示符)...

> cygpath -m c:\some\path
c:/some/path

As you can guess, it converts backslashes to slashes.

你可以猜到,它将反斜杠转换为斜杠。

#3


0  

@Alexandru & Jeremy, Thanks for your help. You both get upvotes

@Alexandru&Jeremy,谢谢你的帮助。你们都得到了赞成

@Jeremy

Using your method I got the following error:

使用您的方法我收到以下错误:

sed: -e expression #1, char 8: unterminated `s' command

sed:-e expression#1,char 8:unterminated`s'命令

If you can edit your answer to make it work I'd accept it. (pasting my solution doesn't count)

如果您可以编辑您的答案以使其正常工作,我会接受它。 (粘贴我的解决方案不算)

Update: Ok, I tried it with UnixUtils and it worked. (For reference, the UnixUtils I downloaded was dated March 1, 2007, and uses GNU sed version 3.02, my Cygwin install has GNU sed version 4.1.5)

更新:好的,我尝试使用UnixUtils并且它有效。 (作为参考,我下载的UnixUtils日期为2007年3月1日,使用GNU sed版本3.02,我的Cygwin安装有GNU sed版本4.1.5)