Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Powershell中的文件中删除一行中的3个空行_Powershell - Fatal编程技术网

如何从Powershell中的文件中删除一行中的3个空行

如何从Powershell中的文件中删除一行中的3个空行,powershell,Powershell,不确定这是否可行,但我想使用Powershell来完成以下任务:删除任何空行块中的前3个空行 例如: hello this is a test this is still a test test 我期望的结果是: hello this is a test this is still a test test 这是可能的吗?< /P> < P>。因此,您正在寻找一个行中有4行的新实例(3行为空白行,而下一行为1行)。您可以使用-replace操作符执行正则表达式(正则表达式

不确定这是否可行,但我想使用Powershell来完成以下任务:删除任何空行块中的前3个空行

例如:

hello

this is a test




this is still a test


test
我期望的结果是:

hello

this is a test

this is still a test


test

这是可能的吗?< /P> < P>。因此,您正在寻找一个行中有4行的新实例(3行为空白行,而下一行为1行)。您可以使用

-replace
操作符执行正则表达式(正则表达式的缩写)匹配和替换

@'
hello

this is a test




this is still a test


test
'@ -replace "(\r?\n){4}","`n"

由于不知道是否会有回车符和换行符,我使用了
(\r?\n){4}
的正则表达式匹配模式,该模式表示查找零个或一个回车符后跟一个换行符,并在一行中查找该分组4次。如果找到,则将其替换为1个新行字符。这个输出正是您想要的。这将对多行字符串起作用,因此,如果在文件上执行此操作,则当使用“代码> >获取内容< /代码> .< /p> < p>时,您将希望使用<代码> -Wave<代码>参数。因此,您正在寻找一个行中有4行新行的实例(3行为空白行,而下一行为1行)。您可以使用
-replace
操作符执行正则表达式(正则表达式的缩写)匹配和替换

@'
hello

this is a test




this is still a test


test
'@ -replace "(\r?\n){4}","`n"
由于不知道是否会有回车符和换行符,我使用了
(\r?\n){4}
的正则表达式匹配模式,该模式表示查找零个或一个回车符后跟一个换行符,并在一行中查找该分组4次。如果找到,则将其替换为1个新行字符。这个输出正是您想要的。这将适用于多行字符串,因此,如果对文件执行此操作,则在使用
Get Content

nice one:)+1.nice one:)+1读取文件时,需要使用
-raw
参数。