Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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解析文本文件中的文本_Powershell_Parsing - Fatal编程技术网

Powershell解析文本文件中的文本

Powershell解析文本文件中的文本,powershell,parsing,Powershell,Parsing,我有一个大的文本文件(4000行),我需要解析和匹配一个特定的字符串。在我点击那个字符串之后,我需要它继续向下,直到它再次匹配,并且中间的所有文本我需要取出并保存到它自己的文件中。如何匹配多行而不是一行 我曾尝试使用Select String,但在我的特定实例中无法使用,因此我被卡住了 示例文本文件: SOF I need this from here sample text sample text sample text sample text sample text sample

我有一个大的文本文件(4000行),我需要解析和匹配一个特定的字符串。在我点击那个字符串之后,我需要它继续向下,直到它再次匹配,并且中间的所有文本我需要取出并保存到它自己的文件中。如何匹配多行而不是一行

我曾尝试使用
Select String
,但在我的特定实例中无法使用,因此我被卡住了

示例文本文件:

SOF
I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
sample text 
To here
I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
To here
.
.
.
.
.
.
EOF

1KB的文件非常小,而且很容易一次读入内存。您完全可以将其作为一个多行字符串读入,并将其拆分为输出块

$RawText = Get-Content C:\Path\To\File.txt -Raw
$Records = $RawText -split '[\r\n]+(?=I need this from here)'
For($i=0;$i -lt $Records.count;$i++){
    $Records[$i] | Set-Content C:\Path\To\FileSplit-$i.txt
}
这将为您(以及您提供的示例文本)提供3个文件:

FileSplit-0.txt

SOF
FileSplit-1.txt

I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
sample text 
To here
FileSplit-2.txt

I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
To here
.
.
.
.
.
.
EOF

1KB的文件非常小,而且很容易一次读入内存。您完全可以将其作为一个多行字符串读入,并将其拆分为输出块

$RawText = Get-Content C:\Path\To\File.txt -Raw
$Records = $RawText -split '[\r\n]+(?=I need this from here)'
For($i=0;$i -lt $Records.count;$i++){
    $Records[$i] | Set-Content C:\Path\To\FileSplit-$i.txt
}
这将为您(以及您提供的示例文本)提供3个文件:

FileSplit-0.txt

SOF
FileSplit-1.txt

I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
sample text 
To here
FileSplit-2.txt

I need this from here
sample text 
sample text 
sample text 
sample text 
sample text 
To here
.
.
.
.
.
.
EOF

由于它足够小,可以一次性读入内存,另一个可行的解决方案是将regex模式与regex类的static
matches()
方法结合使用

我已经更新了您的示例文本,以清楚地显示提取的相应行

$file = New-TemporaryFile

@'
SOF
I need this from here
1 sample text 
2 sample text 
3 sample text 
4 sample text 
5 sample text 
6 sample text 
To here
I need this from here
7 sample text 
8 sample text 
9 sample text 
10 sample text 
11 sample text 
To here
.
.
.
.
.
.
EOF
'@ | Set-Content $file -Encoding UTF8

$text = Get-Content $file -raw

[regex]$regex = '(?s)(?<=I need this from here).+?(?=\r?\nTo here)'

$regex.Matches($text) | ForEach-Object {$_.value}
还是这个

$text = Get-Content $file -raw

[regex]$regex = '(?s)(?<=I need this from here).+?(?=\r?\nTo here)'

$matchedtext = $regex.Matches($text)

for($i = 1; $i -le $matchedtext.count; $i++){
    $outfile = Join-Path c:\temp SplitText$i.txt
    Set-Content -Path $outfile -Value $matchedtext[$i].value
    Write-Host Output file: $outfile
}
$text=获取内容$file-raw

[regex]$regex='(?s)(?由于它足够小,可以一次性读入内存,另一个可行的解决方案是将regex模式与regex类的“static
matches()
方法”结合使用

我已经更新了您的示例文本,以清楚地显示提取的相应行

$file = New-TemporaryFile

@'
SOF
I need this from here
1 sample text 
2 sample text 
3 sample text 
4 sample text 
5 sample text 
6 sample text 
To here
I need this from here
7 sample text 
8 sample text 
9 sample text 
10 sample text 
11 sample text 
To here
.
.
.
.
.
.
EOF
'@ | Set-Content $file -Encoding UTF8

$text = Get-Content $file -raw

[regex]$regex = '(?s)(?<=I need this from here).+?(?=\r?\nTo here)'

$regex.Matches($text) | ForEach-Object {$_.value}
还是这个

$text = Get-Content $file -raw

[regex]$regex = '(?s)(?<=I need this from here).+?(?=\r?\nTo here)'

$matchedtext = $regex.Matches($text)

for($i = 1; $i -le $matchedtext.count; $i++){
    $outfile = Join-Path c:\temp SplitText$i.txt
    Set-Content -Path $outfile -Value $matchedtext[$i].value
    Write-Host Output file: $outfile
}
$text=获取内容$file-raw


[regex]$regex='(?s)(?有多大?有多大?能一次读入内存吗?我们需要用流读取器或其他什么东西试着一次读一行吗?如果只是几个兆,你可以把它作为一个大的多行字符串读入内存,然后用正则表达式前瞻性地将它拆分为新行字符,以获得“我需要从这里开始”字符串。如果它的大小是Gig,你可以需要一个流阅读器和更多的逻辑。4000行,我想在不是非常大的领域。1KBHow big是“巨大的”?有多大?能一次读入内存吗?我们需要用流读取器或其他什么东西试着一次读一行吗?如果只是几个兆,你可以把它作为一个大的多行字符串读入内存,然后用正则表达式前瞻性地将它拆分为新行字符,以获得“我需要从这里开始”字符串。如果它的大小是Gig,你可以需要一个流读取器和更多的逻辑。4000行,我猜在不太大的范围内。我得到一个错误的设置内容:输入对象不能绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性与接受管道的任何参数都不匹配“行输入”但我相信它是在创建特定的文件,它只是不把内容放在它们里面,而把它们放在我指定的文件中directory@GarrettStarkey我误解了吗?我以为你说你想要两行之间的文本,而不是所有的文本在几个点上分开?对不起,反应太慢了,你明白了吗问题?鉴于给定的示例文本和我提供的代码,我无法复制错误。我得到一个错误“Set Content:输入对象无法绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性与接受管道输入的任何参数都不匹配但我相信它是在创建特定的文件,它只是没有将内容放在文件中,而是将它们放在我指定的文件中directory@GarrettStarkey我误解了吗?我以为你说你想要两行之间的文本,而不是所有的文本在几个点上分开?对不起,反应太慢了,你找到问题了吗?鉴于给出的示例文本和我提供的代码,我无法复制错误。它运行并创建文件,但不会在文件中放入任何内容。如果有任何区别,“从这里”到“这里”将是同一个词。它们之间的所有内容我都需要。基本上是一个销售订单,我只是将它们分成不同的部分t文件而不是一个大文件我在这里输入的代码绝对会创建包含内容的两个文件。现在,如果您有不同的输入文件和不同的单词进行匹配,则将由您来设计模式。我只有您提供的可供使用的内容。我对您的答案进行了更多的审阅,我知道哪里出了问题!我感谢您所有的helpHappy来帮忙!祝你度过愉快的一天。它运行并创建文件,但不会在文件中放入任何内容。如果有什么不同,“从这里”到“这里”将是同一个词。它们之间的所有内容我都需要。基本上这是一个销售订单,我只是将它们拆分成不同的文件,而不是我在这里输入的代码中的一个大文件绝对会创建包含内容的两个文件。现在,如果您有不同的输入文件和不同的单词来匹配,则将由您来创建模式。我只有您提供的内容可供使用。我再次查看了您的答案,我知道我错在哪里了!我感谢您的所有帮助高兴地提供帮助!祝您度过愉快的一天。