2013-04-28 45 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