如何将复杂的 sed 命令转换为 PowerShell 脚本 [英] How to convert complex sed commands to a PowerShell script
问题描述
我有一个 Windows 批处理文件,它使用 sed
来操作文件.操作包括简单的替换,如
I have a Windows batch file the uses sed
to manipulate files. The manipulation includes simple substitutions like
sed -e s/PATTERN/OTHERS/g infile >outfile
sed -e "/^.*COPY_START/,/^.*COPY_END/ d" infile >outfile
虽然第一个可以替换为
Get-Content infile | %{ $_ -replace 'PATTERN', 'OTHERS' } | Set-Content outfile
我没有找到如何选择由行标记 COPY_START
和 COPY_END
删除的一堆行.
I didn't found how to select a bunch of lines that shall be removed by the line markers COPY_START
and COPY_END
.
如何用 PowerShell 替换这些 sed
命令.
How can these sed
commands be replaced with PowerShell.
关于杰夫的评论,我试图用这段代码捕捉多行问题:
Regarding Jeff's comment I tried to catch the multi-line problem with this code:
Get-Content infile -replace '(.*)COPY_START\n.*COPY_END(.*)','$1$2'
但是 \n
不满足搜索条件.为了隔离我使用的多行"标准
But the \n
does not fulfill the search criteria. To isolate the "multi-line" criteria I used
Get-Content infile -match 'COPY_START\n.*COPY_END'
使用这个文件
before
do COPY_START
skip
until COPY_END
after
但匹配总是$False
.我希望表达式 \n.*
应该与 COPY_START 之后的行结束以及直到 COPY_END 出现的所有行匹配.
But the match is always $False
. I expected that the expression \n.*
should match with the line end after COPY_START and all lines until COPY_END appears.
为什么与正则表达式不匹配,我该如何解决?
Why doesn't match the regular expression and how can I fix it?
推荐答案
sed -e "/^.*COPY_START/,/^.*COPY_END/d" infile >outfile
命令找到COPY_START
和 COPY_END
行之间的所有子字符串,并将它们从 infile
中删除,并将输出保存到 outfile
.
The sed -e "/^.*COPY_START/,/^.*COPY_END/ d" infile >outfile
command finds all substrings between lines having COPY_START
and COPY_END
and removes them from the infile
and saves the output to outfile
.
要使其在 PS 中工作,您需要使用 -Raw
将文件读入单个变量,并使用像
To make it work in PS, you need to read the file into a single variables with -Raw
and use a regex like
(?m)^.*COPY_START(?s:.*?)COPY_END.*$\n?
请参阅正则表达式演示.
详情
(?m)
- 一个多行内联修饰符,^
现在将匹配一行的开头^
- 行开始.*
- LF 以外的任意 0+ 个字符,尽可能多COPY_START
- 一个子字符串(?s:.*?)
- 内联修饰符组(其中s
启用RegexOptions.Singleline
选项,也称为DOTALL,当.
匹配任何字符) 匹配任何 0+ 个字符时,尽可能少到第一个COPY_END.*$
-COPY_END
然后是尽可能多的除换行符以外的任何 0+ 个字符\n?
- 和一个可选的换行符.
(?m)
- a MULTILINE inline modifier,^
will match the start of a line now^
- line start.*
- any 0+ chars other than LF, as many as possibleCOPY_START
- a substring(?s:.*?)
- an inline modifier group (wheres
enables theRegexOptions.Singleline
option, also known as DOTALL, when.
matches any char) that matches any 0+ chars, as few as possible up to the firstCOPY_END.*$
-COPY_END
and then any 0+ chars other than a newline as many as possible\n?
- and an optional newline.
这篇关于如何将复杂的 sed 命令转换为 PowerShell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!