简体   繁体   English

如果行包含搜索模式,则使用Powershell从文件中提取文本(文件名)

[英]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.

相关问题 使用PowerShell通过从行首匹配模式从csv文件中提取文本行 - Extracting line of text from csv file with PowerShell by matching a pattern at the beginning of the line 使用 PowerShell 和输出文件名搜索正则表达式模式 - Search regex pattern with PowerShell and output filename python:在文件中提取(正则表达式)模式而无需逐行(多行搜索) - python: extracting (regex) pattern in a file without going through line by line (multiline search) 从 R 中的文本文件中提取模式子字符串 - Extracting pattern substrings from a text file in R 将包含搜索模式的行完全替换为另一行 - replace line that contains search pattern entirely with another line Perl:如何逐行搜索文本文件以查找特定模式? - Perl: How do I search a text file line by line to find a specific pattern? 如果找到搜索模式,则 Append 文本位于行尾 - Append text in the end of line if search pattern is found 从具有重复嵌套模式的文本文件中提取文本 - Extracting text from text file with recurring nested pattern 如果正则表达式模式在Java中匹配,则提取文件的下一行数据 - Extracting the next line data of a file if regex pattern matches in Java 搜索包含一些文本的行,替换完整行python - Search for a line that contains some text, replace complete line python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM