2013-04-28 47 views
0

我在批處理文件中使用Windows的GNU Sed。我想使用sed grouping命令{}來分組一組命令。但根據UNIX平臺的文檔,我們必須以新行開始{}中的每個sed命令。可以在UNIX中完成,因爲bash支持將命令跨越1行。但在Windows批處理文件中,我們如何將命令分成> 1行?我知道我可以把所有的sed命令放在一個單獨的腳本文件中,並用'-f script-filename'調用sed。但是我想限制爲只有一個批處理文件,並希望通過使用'sed -e script'選項將所有內容放入批處理文件中。Windows批處理文件和GNU Sed分組命令

如何在批處理文件中使用sed -e指定分組命令?

編輯

例如,從這個http://www.grymoire.com/Unix/Sed.html#uh-35

#!/bin/sh 
# This is a Bourne shell script that removes #-type comments 
# between 'begin' and 'end' words. 
sed -n ' 
    /begin/,/end/ { 
     s/#.*// 
     s/[ ^I]*$// 
     /^$/ d 
     p 
    } 
' 

我們如何做同樣的事情在一個批處理文件?我的意思是,當他們將sed移植到Windows平臺時,他們應該遇到了這個問題並找出瞭解決方法。除非GNU人決定在批處理文件中不支持{},但是我無法在GNU Sed文檔中找到任何內容。

+0

請給多行命令的例子。給出 – Endoro 2013-04-28 08:46:15

+1

例如,看到我的編輯 – JavaMan 2013-04-28 08:51:56

回答

0

您可以使用PowerShell執行此操作,但不能使用cmd.exe。

echo ' 
hello 
world 
' 

結果

 
PS C:\Users\Steven> ./foo.ps1 

hello 
world 
0

這個作品在GnuSed windows下。

SED -f file.sed file.txt的

這file.sed

# sed script to convert +this+ to [i]this[/i] 
:a 
/+/{ x;  # If "+" is found, switch hold and pattern space 
    /^ON/{  # If "ON" is in the (former) hold space, then .. 
    s///;  # .. delete it 
    x;   # .. switch hold space and pattern space back 
    s|+|[/i]|; # .. turn the next "+" into "[/i]" 
    ba;  # .. jump back to label :a and start over 
    } 
s/^/ON/;  # Else, "ON" was not in the hold space; create it 
x;    # Switch hold space and pattern space 
s|+|[i]|;  # Turn the first "+" into "[i]" 
ba;   # Branch to label :a to find another pattern 
} 
#---end of script--- 
+0

我正在尋找一種方法,把所有的東西直接在批處理文件中file.sed。所以使用「sed -f file.sed ..」並不是我想要的。我想將所有內容嵌入到單個批處理文件中。 – JavaMan 2013-04-28 12:03:12

+1

可以呼應陳述出來到一個臨時文件並運行-f開關的sed。這是一種久經考驗的真正的批處理技術,並允許您將其全部保留在一個批處理文件中。 GnuSED -e交換機在Windows中的工作原理也是FWIW。 – foxidrive 2013-04-28 13:17:22