Cygwin 替换两个特定字符串之间的行-cmd中的sed等效

Cygwin 替换两个特定字符串之间的行-cmd中的sed等效,cygwin,cmd,powershell,text-processing,Cygwin,Cmd,Powershell,Text Processing,我想替换两个字符串[REPORT]和[TAGS]之间的行。文件看起来像这样 我在以下范围内使用了sed: 这给了我这个: 当我这样做并在记事本中打开输出文件时,它不会显示新行。我假设这是因为格式化问题,一个简单的Dos2Unix应该可以解决这个问题 但正因为如此,而且主要是因为并非我的所有同事都可以访问cygwin,我想知道是否有办法在中实现这一点(或者如果无法批量执行Powershell) 最后,我希望在大量文件上运行此命令,并将其中的这一部分(在上述两个单词之间)更改为我提供的文本。使用Po

我想替换两个字符串
[REPORT]
[TAGS]
之间的行。文件看起来像这样

我在以下范围内使用了
sed

这给了我这个:

当我这样做并在记事本中打开输出文件时,它不会显示新行。我假设这是因为格式化问题,一个简单的
Dos2Unix
应该可以解决这个问题

但正因为如此,而且主要是因为并非我的所有同事都可以访问
cygwin
,我想知道是否有办法在中实现这一点(或者如果无法批量执行
Powershell


最后,我希望在大量文件上运行此命令,并将其中的这一部分(在上述两个单词之间)更改为我提供的文本。

使用PowerShell,从Windows 7上提供

## Q:\Test\2018\10\30\SO_53073481.ps1
## defining variable with a here string
$Text = @"
Many lines 
many lines
they remain the same

[REPORT]

some text
some more text412

[TAGS]

text that I Want
to stay the same!!!
"@

$Text -Replace "(?sm)(?<=^\[REPORT\]`r?`n).*?(?=`r?`n\[TAGS\])",
               "`nmy text goes here`nAnd a new line down here`n"

要从文件中读取文本、替换和写回(即使不存储在var中),您可以使用:

(Get-Content ".\file.txt" -Raw) -Replace "(?sm)(?<=^\[REPORT\]`r?`n).*?(?=`r?`n\[TAGS\])",
               "`nmy text goes here`nAnd a new line down here`n"|
Set-Content ".\file.txt"
(获取内容“\file.txt”-原始)-替换“(?sm)(?)?
使用

cscript //nologo "C:\Folder\Replace.vbs" < "C:\Windows\Win.ini" > "%userprofile%\Desktop\Test.txt"
cscript//nologo“C:\Folder\Replace.vbs”<“C:\Windows\Win.ini”>%userprofile%\Desktop\Test.txt

所以你可以使用你的正则表达式。

它是powershell,对吗?(我会在答案的顶部提到它,只是为了它)干杯。这是如何替换关键字/模式之间的句子的?与SED的方法相同。全局属性表示处理整个文件。使用您的模式。因此,我可以测试它并确保它工作。我将CRLF添加到Windows文本文件的行尾,或将LF转换为Unix文件的CRLF(您也要求过)。这称为示例代码。
## Q:\Test\2018\10\30\SO_53073481.ps1
## defining variable with a here string
$Text = @"
Many lines 
many lines
they remain the same

[REPORT]

some text
some more text412

[TAGS]

text that I Want
to stay the same!!!
"@

$Text -Replace "(?sm)(?<=^\[REPORT\]`r?`n).*?(?=`r?`n\[TAGS\])",
               "`nmy text goes here`nAnd a new line down here`n"
Many lines
many lines
they remain the same

[REPORT]

my text goes here
And a new line down here

[TAGS]

text that I Want
to stay the same!!!
(Get-Content ".\file.txt" -Raw) -Replace "(?sm)(?<=^\[REPORT\]`r?`n).*?(?=`r?`n\[TAGS\])",
               "`nmy text goes here`nAnd a new line down here`n"|
Set-Content ".\file.txt"
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set regEx = New RegExp
regEx.Pattern = "\n"
regEx.IgnoreCase = True 
regEx.Global = True
Outp.Write regEx.Replace(Inp.ReadAll, vbcrlf)   
cscript //nologo "C:\Folder\Replace.vbs" < "C:\Windows\Win.ini" > "%userprofile%\Desktop\Test.txt"