[英]Extracting text (filename) out of a file with Powershell if line contains search pattern
I´ve got a problem... I´ve got a file where the content looks like 我有一个问题...我有一个文件,内容看起来像
IMPORT ("$(#T_Company-BAG)\KSAKTE13","06141030.eou")
IMPORT ("$(#T_Company-Gesmbh)\KSAKTE13","06141032.eou")
IMPORT ("$(#T_Company-ITALIA)\KSAKTE13","06141038.eou")
IMPORT ("$(#T_Company-ITALIA)\KSAKTE13","06141045.eou")
IMPORT ("$(#T_Company-ITALIA)\RWRECH13","06141512.eou")
The thing i want to do is to extract the file name ( *.eou
) which is inside the last quotes and only the file names which line contains the string T_Company-ITALIA
... 我要做的是提取最后一个引号内的文件名(
*.eou
),仅提取包含字符串T_Company-ITALIA
的文件名...
The first part, extracting all lines containing the search pattern isn´t so difficult... 第一部分,提取包含搜索模式的所有行并不那么困难...
gc -Path C:\Scripts\Easyarchiv\level2.ebt | Select-String -Pattern T_Company-ITALIA
But i don´t know how to get only the file names ( *.eou
) out of the already selected lines... 但是我不知道如何从已选择的行中仅获取文件名(
*.eou
)...
Now I´m searching for a regex which can extract this 现在,我正在寻找可以提取此内容的正则表达式
Here's an option without using Select-String
: 这是一个不使用
Select-String
的选项:
Get-Content file.txt |
where {$_ -match 'T_Company-ITALIA'} |
foreach { $_ -replace '^.+,"(.+)"\).*$','$1'}
试试这个而不是Select-String
:
... | ? { $_ -match 'T_Company-ITALIA.*?,"(.*?)"' } | % { $matches[1] }
If you wanted to stay away from regular expressions (which can be hard to read, debug, and understand) you could do this: 如果您想远离正则表达式(可能难以阅读,调试和理解),则可以这样做:
gc file.txt |
foreach {
$splitArray = $_ -split '"'; # split at the quotation marks
if ($splitArray[1] -match "T_Company-ITALIA")
{$splitArray[3]}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.