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
Regex 使用PowerShell从多个子目录的多个文件中提取文本并输出到文件_Regex_Powershell - Fatal编程技术网

Regex 使用PowerShell从多个子目录的多个文件中提取文本并输出到文件

Regex 使用PowerShell从多个子目录的多个文件中提取文本并输出到文件,regex,powershell,Regex,Powershell,我正在尝试将一系列文本文件中的文本提取到.txt文档中。文本文件存储在桌面上的一系列子目录中 到目前为止,我可以使用正则表达式从特定文本文件中获取变量,但我需要它搜索跨越多个目录的多个文件,而不是我将其指向的单个文件 $input_path = "C:\Users\Darren\Desktop\FolderofData\DataFile1.txt" $regex = '(\W"_[a-z]+\W)' $output_file = "C:\Users\Darren\Desktop\Extracte

我正在尝试将一系列文本文件中的文本提取到.txt文档中。文本文件存储在桌面上的一系列子目录中

到目前为止,我可以使用正则表达式从特定文本文件中获取变量,但我需要它搜索跨越多个目录的多个文件,而不是我将其指向的单个文件

$input_path = "C:\Users\Darren\Desktop\FolderofData\DataFile1.txt"
$regex = '(\W"_[a-z]+\W)'
$output_file = "C:\Users\Darren\Desktop\Extracted_Data.txt"
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

这在PowerShell中可能吗?

您可以通过管道将
FileInfo
对象(如从
Get ChildItem
返回的对象)传输到
Select String

Get-ChildItem $home\Desktop -Filter Data*.txt |Select-String -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

您可以通过管道将
FileInfo
对象(如从
Get ChildItem
返回的对象)传输到
Select String

Get-ChildItem $home\Desktop -Filter Data*.txt |Select-String -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

对于多个目录,get childitem需要-recurse对于多个目录,get childitem需要-recurse