[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创教程] powershell实现linux下面grep命令

本帖最后由 gflrlm 于 2018-4-7 19:31 编辑

以前一直困扰于windows下面没有grep命令,就自己写了一个, 后来发现有GNU grep for windows版本。。。 当然了,,还是自己的好用,是图形化选择文件夹的界面。
使用的时候,首先提示选择文件夹, 然后输入要搜索的字符串,  注意这里支持类似grep aaa -rwi  这种格式, 输入字符串用法参考如下:
your_search_string        # 只搜索该字符串
your_search_string -w   # 搜索该字符串,全字匹配
your_search_string -i   #  搜索该字符串,不区分大小写
your_search_string -wi   # 搜索该字符串,全字匹配,不区分大小写


注意: 如果搜索的字符串含有空格,暂时不支持, 因为本人写的时候没有这个需求,所以没有实现该功能。


为了提高search的效率,使用了如下代码, 每找到一个文件,就立即开始搜索,而不是等把所有的文件全部都获取到之后, 这样就类似于实现了linux下面 find . -type f | xargs -n1 grep xxx 这种作用 。。


,@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} )
  1. $default_path = ""
  2. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  3. while(1)
  4. {
  5.   $Path ="D:\"
  6.   $Filter = '*.*'
  7.   $_Debug = 0
  8.   $DEBUG_LEVEL = 0
  9.   
  10.   Write-Host "请选择要搜索的文件夹路径:例如  D:\test"
  11.   if ($_Debug) {
  12.      $Path = "D:\___sv_cpp_py\vcs\uvm-1.2\src"
  13.   } else {
  14.      #$Path = Read-Host
  15.      
  16.      $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
  17.      $objForm.RootFolder = "MyComputer"
  18.      $objForm.Description = "选择需要搜索的文件夹: "
  19.      $objForm.ShowNewFolderButton = "true"
  20.      $objForm.SelectedPath = "d:\___sv_cpp_py\vcs"
  21.      
  22.      if($default_path -eq ""){
  23. }else {
  24.    $objForm.SelectedPath = $default_path
  25. }
  26.      $Show = $objForm.ShowDialog()
  27.      If ($Show -eq "OK")
  28.      {
  29.        #Return $objForm.SelectedPath
  30.        $Path = $objForm.SelectedPath
  31.    $default_path = $objForm.SelectedPath
  32.      }
  33.      Else
  34.      {
  35.       Write-Warning 'Cancelled'
  36.      }
  37.   }
  38.   #if([String]::IsNullOrEmpty($Path))
  39.   
  40.   if(!(Test-Path $Path))
  41.   {
  42.      $Path =".\"
  43.      Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path
  44.   }
  45.   Write-Host ""
  46.   Write-Host ""
  47.   Write-Host "Path   = " $Path
  48.   if ($_Debug) {
  49.      $MySearchString_tmp = "domain "
  50.   } else {
  51.      Write-Host "请输入要搜索的字符串:"
  52.      $MySearchString_tmp = Read-Host
  53.   }
  54.   while([String]::IsNullOrEmpty($MySearchString_tmp))
  55.   {
  56.      Write-Host "The string is null or empty."
  57.      Write-Host "请重新输入要搜索的字符串:"
  58.      $MySearchString_tmp = Read-Host
  59.   }
  60.   Write-Host "String = " $MySearchString_tmp
  61.   Write-Host ""
  62.   Write-Host ""
  63.   $mySearchStr = $MySearchString_tmp.split(" "); # get -rwi option , just like  grep aaa -rwi *;
  64.   Foreach($split_ele in $mySearchStr)
  65.   {  
  66.   #    write-host "split_ele=" $split_ele
  67.   }
  68.   
  69.   
  70.              $MySearchString_tmp        = $mySearchStr[0]
  71.              $MySearchString_option_rwi = $mySearchStr[1]
  72.   Write-Host "MySearchString_tmp        =" $MySearchString_tmp
  73.   Write-Host "MySearchString_option_rwi =" $MySearchString_option_rwi
  74.   # 电锯惊魂
  75.   # $MySearchString = "*Mad.Max.1979.*"
  76.   $MySearchString    = "*$MySearchString_tmp*"
  77.   $MySearchString_w  = "\b$MySearchString_tmp\b"
  78.   $MySearchString_i  = "$MySearchString_tmp"
  79.   $MySearchString_wi = "\b$MySearchString_tmp\b"
  80.   Write-Host "MySearchString            =" $MySearchString
  81.   Write-Host "MySearchString_w          =" $MySearchString_w
  82.   Write-Host "MySearchString_i          =" $MySearchString_i
  83.   Write-Host "MySearchString_wi         =" $MySearchString_wi
  84.   
  85. function processing_grep(  $file ){
  86.   ### Write-Host "Processing :    $file "
  87.       if($DEBUG_LEVEL -gt 0) { write-host "file_name=" $file }
  88.     $tmpContent = Get-Content $file
  89.       if($DEBUG_LEVEL -gt 0) {write-host "file_length="  $tmpContent.length }
  90.       if($tmpContent.length -gt 0){
  91.         for ($i=0; $i -le $tmpContent.length; $i++)
  92.         {
  93.           if($MySearchString_option_rwi -match "wi") {
  94.                 if($DEBUG_LEVEL -gt 0) { Write-Host "++++++++++MySearchString_wi  like wi +++++++++++" }
  95.                 #exit
  96.          
  97.             if($tmpContent[$i] -cmatch $MySearchString_wi) 
  98.              {
  99.               #write-host ""
  100.               #write-host $file
  101.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  102.                     $found = 1;
  103.              }
  104.                } elseif  ($MySearchString_option_rwi -match "i") {
  105.             if($tmpContent[$i] -cmatch $MySearchString_i) 
  106.              {
  107.               #write-host ""
  108.               #write-host $file
  109.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  110.                     $found = 1;
  111.              }
  112.                } elseif  ($MySearchString_option_rwi -match "w") {
  113.             if($tmpContent[$i] -match $MySearchString_w) 
  114.              {
  115.               #write-host ""
  116.               #write-host $file
  117.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  118.                     $found = 1;
  119.              }
  120.                } else {
  121.             if($tmpContent[$i] -like "$MySearchString") 
  122.              {
  123.               #write-host ""
  124.               #write-host $file
  125.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  126.                     $found = 1;
  127.              }
  128.                }#if -wi
  129.          }#for
  130.         }#if tmpContent.length
  131.   }
  132.   $found = 0;
  133.   #只显示文件,不显示文件夹
  134. ,@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} )
  135.   if ($found -eq 0)
  136.   {
  137.       write-host "没有找到您查找的字符串,请重新输入吧,O(∩_∩)O~ "  -ForegroundColor red -background black
  138.   }
  139.    
  140.   cmd /c "pause"
  141.   # Start-Sleep -s 10
  142. }
复制代码

谢谢分享成果

TOP

如果搜索的字符串含有空格,暂时不支持福彩3Dhttps://1680380.com/view/fc3D/index.html, 因为本人写的时候没有这个需求,所以没有实现该功能。为了提高search的效率,使用了如下代码, 每找到一个文件,就立即开始搜索幸运飞艇https://1680380.com/view/xingyft/pk10kai.html,而不是等把所有的文件全部都获取到之后, 这样就类似于实现了linux下面 find . -type f | xargs -n1 grep xxx 这种作用

TOP

返回列表