0

I am using GNU Sed for Windows in a batch file. I want to use the sed grouping command {} to group a set of commands. But according to the documentation for the UNIX platform, we must start each sed command within the {} in a new line. The can be done in UNIX since bash supports spanning a command into >1 lines. But in a Windows batch file, how do we split a command into >1 lines? I know I can put all sed commands in a separate script file and invoke sed with '-f script-filename'. But I want to restrict to just 1 batch file and want to place everything inside the batch file by using the 'sed -e script' option.

How do we specify grouping command using sed -e inside a batch file?

EDIT

For example, from this 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
    }
'

How do we do the same thing in a batch file? I mean when they port sed to windows platform, they should have encountered this issue and figured out a workaround for it. Unless, the GNU guys decided the {} is not supported in a batch file but I cant find anything in the GNU Sed documentation.

JavaMan
  • 4,954
  • 4
  • 41
  • 69

2 Answers2

0

You can do this with PowerShell, but not cmd.exe.

echo '
hello
world
'

Result

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

hello
world
Zombo
  • 1
  • 62
  • 391
  • 407
0

This works in GnuSed under windows.

sed -f file.sed file.txt

this is 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---
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I'm looking for a way to put all the stuff in file.sed directly inside the batch file. So using "sed -f file.sed.." is not what I want. I want to embed everything into 1 single batch file. – JavaMan Apr 28 '13 at 12:03
  • 1
    You can echo the statements out to a temporary file and run sed with the -f switch. That is a tried and true batch technique, and allows you to keep it all within one batch file. The GnuSED -e switch works in Windows too FWIW. – foxidrive Apr 28 '13 at 13:17