首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带行输出的Textpattern搜索

带行输出的Textpattern搜索
EN

Stack Overflow用户
提问于 2019-10-24 13:08:07
回答 1查看 36关注 0票数 0

我是一个网络工程师,我写了一个很小但很有效的PS脚本来搜索日志(或任何文件)来搜索文本模式。现在,这个脚本只输出行、文件名等等。现在,我想扩展脚本,以便当它找到一行时,它会告诉我行、文件名等等,但也告诉我该行的内容。

所以应该是这样的:

代码语言:javascript
复制
LineNumber Filename            Path                           Pattern                                  
---------- --------            ----                           -------                                  
4          190719_Success.log  C:\skripte\190719_Success.log  test 

.log第4行的文本应该出现在这里

代码语言:javascript
复制
5          190719_Success.log  C:\skripte\190719_Success.log  test  

.log第5行的文本应该出现在这里

不好意思,我希望你明白我的意思。

因为我对PS脚本比较陌生,所以我有点迷失了我应该如何实现这个目标,或者如果可能的话。

到目前为止,我的代码如下:

代码语言:javascript
复制
Clear-Host
$Pfad = Read-Host "Bitte Pfad angeben" #Enter Directory Path to Search
$Suchbegriff = Read-Host "Suchbegriff eingeben" #Enter Pattern to search for
New-Item -ItemType directory -Path C:\Skripte -erroraction 'silentlycontinue' #create C:\Skripte Folder
Remove-Item -Path C:\Skripte\Suchergebnis.txt -erroraction 'silentlycontinue' #Cleanup from previous run
Remove-Item -Path C:\Skripte\Indizierung.csv -erroraction 'silentlycontinue' #Cleanup
cd $Pfad 

echo $file.fullname
echo ""
select-string -Path .\*.* -Pattern "$Suchbegriff" -erroraction 'silentlycontinue' | Select-Object LineNumber,Filename,Path,Pattern | ft -wrap #Search the specified Directory
echo ""

while(($Create = Read-Host -Prompt "Unterordner durchsuchen? J für Ja, N für Nein") -ne "x") #Userinput if Subdirectorys should be searched aswell
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     {
 switch ($Create)

{
        'J' 
{
Get-Childitem  -erroraction 'silentlycontinue' | Get-ChildItem -Recurse -erroraction 'silentlycontinue' | Where-Object {$_.PSIsContainer} | Export-CSV -NoClobber -NoTypeInformation -Path C:\Skripte\Indizierung.csv #Get all Subdirectorys and put them in a CSV, two GCI are needed to reliably get all subdirectories.
$Files = import-csv -Delimiter ',' -Path C:\Skripte\Indizierung.csv #Import CSV
foreach ($File in $Files)
{
cd $file.fullname
echo $file.fullname
echo ""
select-string -Path .\*.* -Pattern "$Suchbegriff" -erroraction 'silentlycontinue' | Select-Object LineNumber,Filename,Path,Pattern | ft -wrap
echo ""
pause
exit
} 
}
        'n'
{
pause
Exit
}       
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-24 14:06:40

看看下面的代码。我添加了一个处理文件的循环,以及对输入的一些验证+将行内容显示到控制台的请求。

代码改编(@Theo:谢谢输入)

代码语言:javascript
复制
#Get input
[System.String]$SearchPath = Read-Host -Prompt 'Enter path'
[System.String]$SearchPattern = Read-Host -Prompt 'Enter search pattern'
[System.String]$SearchRecurse = Read-Host -Prompt 'Search recurse (Y/N)'

#Validate input
if ((-not $SearchPath) -or (-not (Test-Path -Path $SearchPath)))
{
    throw ('Path "' + $SearchPath + 'is not available!')
}

if ((-not $SearchPattern))
{
    throw ('Search pattern is empty!')
}

if (('Y', 'N') -notcontains $SearchRecurse)
{
    throw ('Search recurse parameter "' + $SearchRecurse + ' is not valid!')
}

#Get all files
Out-Host -InputObject 'Get all files...'
[PSCustomObject[]]$Files = @()
if ($SearchRecurse -eq 'Y')
{
    $Files = Get-ChildItem -Path $SearchPath -File -Recurse -Force #Collect also files from subfolders
}
else
{
    $Files = Get-ChildItem -Path $SearchPath -File -Force #Collect only files from the current folder
}

#Search for string
Out-Host -InputObject 'Search for string...'
[PSCustomObject[]]$Output = @()
foreach ($File in ($Files)) #Process each file
{
    Out-Host -InputObject $File.FullName
    $Output += Select-String -Path $File.FullName -Pattern $SearchPattern | Select-Object -Property LineNumber, Filename, Path, Pattern, Line
}

$Output | Format-Table -Wrap
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58542091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档