找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23227|回复: 2

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

[复制链接]
发表于 2018-4-7 19:27:12 | 显示全部楼层 |阅读模式
本帖最后由 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.          
  28.      If ($Show -eq "OK")
  29.      {
  30.        #Return $objForm.SelectedPath
  31.        $Path = $objForm.SelectedPath
  32.            $default_path = $objForm.SelectedPath
  33.      }
  34.      Else
  35.      {
  36.       Write-Warning 'Cancelled'
  37.      }

  38.   }
  39.   #if([String]::IsNullOrEmpty($Path))
  40.   
  41.   if(!(Test-Path $Path))
  42.   {
  43.      $Path ="."
  44.      Write-Host "您输入文件夹路径不存在,取当前目录查找:Path = "$Path
  45.   }

  46.   Write-Host ""
  47.   Write-Host ""
  48.   Write-Host "Path   = " $Path

  49.   if ($_Debug) {
  50.      $MySearchString_tmp = "domain "
  51.   } else {
  52.      Write-Host "请输入要搜索的字符串:"
  53.      $MySearchString_tmp = Read-Host
  54.   }

  55.   while([String]::IsNullOrEmpty($MySearchString_tmp))
  56.   {
  57.      Write-Host "The string is null or empty."
  58.      Write-Host "请重新输入要搜索的字符串:"
  59.      $MySearchString_tmp = Read-Host
  60.   }

  61.   Write-Host "String = " $MySearchString_tmp
  62.   Write-Host ""
  63.   Write-Host ""

  64.   $mySearchStr = $MySearchString_tmp.split(" "); # get -rwi option , just like  grep aaa -rwi *;
  65.   Foreach($split_ele in $mySearchStr)
  66.   {  
  67.   #    write-host "split_ele=" $split_ele
  68.   }
  69.   
  70.   

  71.              $MySearchString_tmp        = $mySearchStr[0]
  72.              $MySearchString_option_rwi = $mySearchStr[1]
  73.   Write-Host "MySearchString_tmp        =" $MySearchString_tmp
  74.   Write-Host "MySearchString_option_rwi =" $MySearchString_option_rwi
  75.   # 电锯惊魂
  76.   # $MySearchString = "*Mad.Max.1979.*"
  77.   $MySearchString    = "*$MySearchString_tmp*"
  78.   $MySearchString_w  = "\b$MySearchString_tmp\b"
  79.   $MySearchString_i  = "$MySearchString_tmp"
  80.   $MySearchString_wi = "\b$MySearchString_tmp\b"
  81.   Write-Host "MySearchString            =" $MySearchString
  82.   Write-Host "MySearchString_w          =" $MySearchString_w
  83.   Write-Host "MySearchString_i          =" $MySearchString_i
  84.   Write-Host "MySearchString_wi         =" $MySearchString_wi
  85.   
  86. function processing_grep(  $file ){
  87.   ### Write-Host "Processing :    $file "

  88.       if($DEBUG_LEVEL -gt 0) { write-host "file_name=" $file }
  89.     $tmpContent = Get-Content $file
  90.       if($DEBUG_LEVEL -gt 0) {write-host "file_length="  $tmpContent.length }
  91.       if($tmpContent.length -gt 0){
  92.         for ($i=0; $i -le $tmpContent.length; $i++)
  93.         {
  94.           if($MySearchString_option_rwi -match "wi") {
  95.                 if($DEBUG_LEVEL -gt 0) { Write-Host "++++++++++MySearchString_wi  like wi +++++++++++" }
  96.                 #exit
  97.          
  98.             if($tmpContent[$i] -cmatch $MySearchString_wi) 
  99.              {
  100.               #write-host ""
  101.               #write-host $file
  102.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  103.                     $found = 1;
  104.              }
  105.                } elseif  ($MySearchString_option_rwi -match "i") {
  106.             if($tmpContent[$i] -cmatch $MySearchString_i) 
  107.              {
  108.               #write-host ""
  109.               #write-host $file
  110.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  111.                     $found = 1;
  112.              }
  113.                } elseif  ($MySearchString_option_rwi -match "w") {
  114.             if($tmpContent[$i] -match $MySearchString_w) 
  115.              {
  116.               #write-host ""
  117.               #write-host $file
  118.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  119.                     $found = 1;
  120.              }
  121.                } else {
  122.             if($tmpContent[$i] -like "$MySearchString") 
  123.              {
  124.               #write-host ""
  125.               #write-host $file
  126.               write-host $file " : " $i " : " $tmpContent[$i] -ForegroundColor yellow -background black
  127.                     $found = 1;
  128.              }
  129.                }#if -wi
  130.          }#for
  131.         }#if tmpContent.length
  132.   }


  133.   $found = 0;
  134.   #只显示文件,不显示文件夹

  135. ,@(Get-ChildItem -Path $Path -Filter $Filter -Recurse | ?{$_.PsIsContainer -eq $false} | %{processing_grep $_.FullName} )

  136.   if ($found -eq 0)
  137.   {
  138.       write-host "没有找到您查找的字符串,请重新输入吧,O(∩_∩)O~ "  -ForegroundColor red -background black
  139.   }
  140.    

  141.   cmd /c "pause"
  142.   # Start-Sleep -s 10
  143. }
复制代码
发表于 2019-10-25 17:07:49 | 显示全部楼层
谢谢分享成果
发表于 2020-8-15 17:32:34 | 显示全部楼层
如果搜索的字符串含有空格,暂时不支持福彩3Dhttps://1680380.com/view/fc3D/index.html, 因为本人写的时候没有这个需求,所以没有实现该功能。为了提高search的效率,使用了如下代码, 每找到一个文件,就立即开始搜索幸运飞艇https://1680380.com/view/xingyft/pk10kai.html,而不是等把所有的文件全部都获取到之后, 这样就类似于实现了linux下面 find . -type f | xargs -n1 grep xxx 这种作用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:41 , Processed in 0.019519 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表