2016-11-01 163 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 %% =%'是無效的。 –