powershell实现linux下面grep命令
[i=s] 本帖最后由 gflrlm 于 2018-4-7 19:31 编辑 [/i]以前一直困扰于windows下面没有grep命令,就自己写了一个, 后来发现有GNU grep for windows版本。。。 当然了,,还是自己的好用,是图形化选择文件夹的界面。
使用的时候,首先提示选择文件夹, 然后输入要搜索的字符串, 注意这里支持类似grep aaa -rwi 这种格式, [color=Red][b]输入字符串用法参考如下: [/b][/color]
[i]your_search_string # 只搜索该字符串
your_search_string -w # 搜索该字符串,全字匹配
your_search_string -i # 搜索该字符串,不区分大小写
your_search_string -wi # 搜索该字符串,全字匹配,不区分大小写[/i]
[b][color=Red]注意: 如果搜索的字符串含有空格,暂时不支持, 因为本人写的时候没有这个需求,所以没有实现该功能。
[/color][/b]
[b][color=Red]为了提高search的效率,使用了如下代码, 每找到一个文件,就立即开始搜索,而不是等把所有的文件全部都获取到之后, 这样就类似于实现了linux下面 find . -type f | xargs -n1 grep xxx 这种作用 。。
[/color][/b]
[b][color=Green][size=4],@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} )[/size][/color][/b][code]$default_path = ""
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
while(1)
{
$Path ="D:\"
$Filter = '*.*'
$_Debug = 0
$DEBUG_LEVEL = 0
Write-Host "请选择要搜索的文件夹路径:例如 D:\test"
if ($_Debug) {
$Path = "D:\___sv_cpp_py\vcs\uvm-1.2\src"
} else {
#$Path = Read-Host
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.RootFolder = "MyComputer"
$objForm.Description = "选择需要搜索的文件夹: "
$objForm.ShowNewFolderButton = "true"
$objForm.SelectedPath = "d:\___sv_cpp_py\vcs"
if($default_path -eq ""){
}else {
$objForm.SelectedPath = $default_path
}
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
#Return $objForm.SelectedPath
$Path = $objForm.SelectedPath
$default_path = $objForm.SelectedPath
}
Else
{
Write-Warning 'Cancelled'
}
}
#if([String]::IsNullOrEmpty($Path))
if(!(Test-Path $Path))
{
$Path =".\"
Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path
}
Write-Host ""
Write-Host ""
Write-Host "Path = " $Path
if ($_Debug) {
$MySearchString_tmp = "domain "
} else {
Write-Host "请输入要搜索的字符串:"
$MySearchString_tmp = Read-Host
}
while([String]::IsNullOrEmpty($MySearchString_tmp))
{
Write-Host "The string is null or empty."
Write-Host "请重新输入要搜索的字符串:"
$MySearchString_tmp = Read-Host
}
Write-Host "String = " $MySearchString_tmp
Write-Host ""
Write-Host ""
$mySearchStr = $MySearchString_tmp.split(" "); # get -rwi option , just like grep aaa -rwi *;
Foreach($split_ele in $mySearchStr)
{
# write-host "split_ele=" $split_ele
}
$MySearchString_tmp = $mySearchStr[0]
$MySearchString_option_rwi = $mySearchStr[1]
Write-Host "MySearchString_tmp =" $MySearchString_tmp
Write-Host "MySearchString_option_rwi =" $MySearchString_option_rwi
# 电锯惊魂
# $MySearchString = "*Mad.Max.1979.*"
$MySearchString = "*$MySearchString_tmp*"
$MySearchString_w = "\b$MySearchString_tmp\b"
$MySearchString_i = "$MySearchString_tmp"
$MySearchString_wi = "\b$MySearchString_tmp\b"
Write-Host "MySearchString =" $MySearchString
Write-Host "MySearchString_w =" $MySearchString_w
Write-Host "MySearchString_i =" $MySearchString_i
Write-Host "MySearchString_wi =" $MySearchString_wi
function processing_grep( $file ){
### Write-Host "Processing : $file "
if($DEBUG_LEVEL -gt 0) { write-host "file_name=" $file }
$tmpContent = Get-Content $file
if($DEBUG_LEVEL -gt 0) {write-host "file_length=" $tmpContent.length }
if($tmpContent.length -gt 0){
for ($i=0; $i -le $tmpContent.length; $i++)
{
if($MySearchString_option_rwi -match "wi") {
if($DEBUG_LEVEL -gt 0) { Write-Host "++++++++++MySearchString_wi like wi +++++++++++" }
#exit
if($tmpContent[$i] -cmatch $MySearchString_wi)
{
#write-host ""
#write-host $file
write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
$found = 1;
}
} elseif ($MySearchString_option_rwi -match "i") {
if($tmpContent[$i] -cmatch $MySearchString_i)
{
#write-host ""
#write-host $file
write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
$found = 1;
}
} elseif ($MySearchString_option_rwi -match "w") {
if($tmpContent[$i] -match $MySearchString_w)
{
#write-host ""
#write-host $file
write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
$found = 1;
}
} else {
if($tmpContent[$i] -like "$MySearchString")
{
#write-host ""
#write-host $file
write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
$found = 1;
}
}#if -wi
}#for
}#if tmpContent.length
}
$found = 0;
#只显示文件,不显示文件夹
,@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} )
if ($found -eq 0)
{
write-host "没有找到您查找的字符串,请重新输入吧,O(∩_∩)O~ " -ForegroundColor red -background black
}
cmd /c "pause"
# Start-Sleep -s 10
}[/code] 谢谢分享成果 **** 作者被禁止或删除 内容自动屏蔽 ****
页:
[1]