英文:
Efficiently extract matching lines including path and line number?
问题
以下代码段仅提取匹配的行,并获取路径和行号:
Get-ChildItem $thePath\ -Include "*.txt" -Recurse | ForEach-Object {
$file = $_.FullName
Get-Content $file | ForEach-Object {
$lineNumber = 1
$_ | Select-String -Pattern 'THE_PATTEN' | ForEach-Object {
"$file:$lineNumber:$($_.Line)" | Out-File -Append "output.txt"
$lineNumber++
}
}
}
请注意将 'THE_PATTEN' 替换为您要匹配的实际模式。此代码将提取包括路径、文件名、行号和匹配行的信息,并将其追加到 "output.txt" 文件中。
英文:
The following snippet extracts only the matching lines, I also want the path and line number:
Get-ChildItem $thePath\ -Include "*.txt" -Recurse | Get-Content | Select-String -Pattern 'THE_PATTEN' | Set-Content "output.txt"
I tried with this method and still it only extracts the matching lines:
Get-ChildItem $thePath\ -Include "*.txt" -Recurse | Get-Content | Select-String -Pattern 'THE_PATTEN' | Select-Object -ExpandProperty Line | Set-Content "output.txt"
How can I extract the path:filename:line number: matching line?
答案1
得分: 2
不需要使用 get-content。路径通过管道传递。(. 用于 -path,*.txt 用于 -filter 以提高速度)
get-childitem -recurse . *.txt | select-string hi
foo2\file3.txt:1:hi
file1.txt:1:hi
file2.txt:1:hi
英文:
You don't need get-content. The path is passed over the pipe. (. is for -path, and *.txt is for -filter for speed)
get-childitem -recurse . *.txt | select-string hi
foo2\file3.txt:1:hi
file1.txt:1:hi
file2.txt:1:hi
答案2
得分: 1
是的,你可以从Select-String
的输出中获取行号和文件名:
ls *.txt | % { Select-String -Path $_ -Pattern "THE_PATTERN" | select-object LineNumber, Line, Path }
你会注意到这种方法也稍微快一些。
祝你好运!
英文:
Yea, you can get line number and file name from the output of Select-String
:
ls *.txt | % { Select-String -Path $_ -Pattern "THE_PATTERN" | select-object LineNumber, Line, Path }
You'll notice this approach is also a touch faster.
Good luck!
答案3
得分: 1
请注意,Get-ChildItem -Filter
比 Get-ChildItem -Include
更高效(参见 help get-childitem
)。接下来是 Select-String
可以接受文件。无需先获取内容。现在只需选择您需要的属性并导出您的文件。(请注意,变量 $match
和 $matches
是系统变量,所以您可能不想使用它们。)
$Patterns = Get-ChildItem $thePath -Filter "*.txt" -Recurse | Select-String -Pattern 'THE_PATTEN' | select Path, Filename, LineNumber, Line
# 导出为 csv(可在 Excel 中使用)
$Patterns | Export-Csv output.csv -NoTypeInformation # -Delimiter ";" # 分隔符是可选的,取决于您的地区
# 导出为 txt
foreach ($Pattern in $Patterns) {
('{0} : {1} : {2}' -f ($Pattern.Path), ($Pattern.LineNumber), ($Pattern.Line)) | Add-Content "output.txt"
}
英文:
First note that Get-ChildItem -Filter
is way more efficient than Get-ChildItem -Include
(see help get-childitem
). Next is that Select-String
accepts files. No need to get the content first. Now just Select
the properties you need and export your file. (Note that the variable $match
and $matches
are system variables so you might not want to use them.)
$Patterns = Get-ChildItem $thePath -Filter "*.txt" -Recurse| Select-String -Pattern 'THE_PATTEN' | select Path,Filename,LineNumber,Line
# Export to csv (usable in excel)
$Patterns | Export-Csv output.csv -NoTypeInformation # -Delimiter ";" # the delimiter is optinal and depending of your region
# Exporting txt
foreach ($Pattern in $Patterns){
('{0} : {1} : {2}' -f ($Pattern.Path),($Pattern.LineNumber),($Pattern.Line)) | Add-Content "output.txt"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论