2016-11-01 120 views
0

我想知道是否可以在批处理文件命令中有注释。具体地讲,我有一个长SED如下命令:多行命令批处理注释

@SED -r -e "s/.../.../"^ 
    -e "s/.../.../"^ 
    -e "s/.../.../"^ 
    fileName >outFileName 

我想将注释添加到每个“-e”选项,如图所示在下面的实施例:

:: Option #1: At the end of the line 
@SED -r -e "s/.../.../"^ // First comment 
    -e "s/.../.../"^  // Second comment 
    -e "s/.../.../"^  // Third comment 
    fileName >outFileName 

:: Option #2: Between lines 
@SED -r 
    @REM First comment 
    -e "s/.../.../"^ 
    @REM Second comment 
    -e "s/.../.../"^ 
    @REM Third comment 
    -e "s/.../.../"^ 
    fileName >outFileName 

有什么办法可以做到这一点?

+0

灿* NIX版本的sed的手柄内联评论? – SomethingDark

回答

5

试试这个。我没有sed,所以我只是用echo进行测试。

@echo off 
:: Option #1: At the end of the line 
echo SED -r -e "s/.../.../" %= First comment =%^ 
    -e "s/.../.../" %= second comment =%^ 
    -e "s/.../.../" %= third comment =% 

:: Option #2: Between lines 
echo SED -r^ 
    %= First comment =%^ 
    -e "s/.../.../"^ 
    %= second comment =%^ 
    -e "s/.../.../"^ 
    %= third comment =%^ 
    -e "s/.../.../" 

pause 

输出

SED -r -e "s/.../.../"  -e "s/.../.../"  -e "s/.../.../" 
SED -r  -e "s/.../.../"  -e "s/.../.../"  -e "s/.../.../" 
Press any key to continue . . . 
+0

如果注释包含'%'字符,有没有办法做到这一点?我想也许我可以将注释更改为'!= My Comment%=!',因为我启用了延迟变量扩展,但这似乎不起作用。 –

+0

@JeffG,对。延迟扩展在执行代码行时扩展变量。在代码执行之前,百分比变量被展开。逃脱%的唯一方法是将其加倍。但在这种情况下,这不会为你工作。 – Squashman

+0

只要每个结果变量未定义,似乎就可以使注释中的括号加倍。例如,'%= %%不存在%% =%'是有效的,因为没有定义'%=%','%不存在%'或'%=%'。但是,自从定义了'%windir%'后,'%= %% windir %% =%'是无效的。 –