0

バッチ ファイルで GNU Sed for Windows を使用しています。sed グループ化コマンド {} を使用して、一連のコマンドをグループ化したいと考えています。ただし、UNIX プラットフォームのドキュメントによると、{} 内の各 sed コマンドを新しい行で開始する必要があります。UNIX で実行できるのは、bash がコマンドを 1 行以上にまたがることをサポートしているためです。しかし、Windows バッチ ファイルでは、コマンドを 1 行以上に分割するにはどうすればよいでしょうか。すべての sed コマンドを別のスクリプト ファイルに入れ、「-f script-filename」で sed を呼び出すことができることを知っています。しかし、バッチ ファイルを 1 つだけに制限し、「sed -e script」オプションを使用してすべてをバッチ ファイル内に配置したいと考えています。

バッチファイル内で 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 のドキュメントには何も見つかりません。

4

2 に答える 2

0

これは PowerShell で実行できますが、cmd.exe では実行できません。

echo '
hello
world
'

結果

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

こんにちは
世界
于 2013-04-28T08:52:53.440 に答える
0

これは Windows の下の GnuSed で機能します。

sed -f ファイル.sed ファイル.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---
于 2013-04-28T11:25:11.503 に答える