powershell_where vs grep

reference

link

special variable:$_.

$_经常使用在管道符中,作为前一个变量的输出对象来作为下一个命令的输入(您可以从中提取需要的属性完成一定的逻辑)
譬如,在作为筛选工具对象的where-object的值表达式中经常会用的$_

Get-Process | Where-Object {
    
    $_.ProcessName -eq ‘dllhost’}

为了提取合适的字段,您或许需要利用其他cmdlet来获取准确的字段.
当然,某系cmdlet自带筛选器,自带筛器一般具有更好的性能

SYNOPSIS
    Selects objects from a collection based on their property values.


    --------------- Example 1: Get stopped services ---------------

    Get-Service | Where-Object {
    
    $_.Status -eq "Stopped"}
    Get-Service | where Status -eq "Stopped"


    -------- Example 2: Get processes based on working set --------

    Get-Process | Where-Object {
    
    $_.WorkingSet -GT 250MB}
    Get-Process | Where-Object WorkingSet -GT (250MB)


    -------- Example 3: Get processes based on process name --------

    Get-Process | Where-Object {
    
    $_.ProcessName -Match "^p.*"}
    Get-Process | Where-Object ProcessName -Match "^p.*"

grep

grep是基于文本流的筛选工具
只需要考虑文本正则表达式的编写即可使用
但是在某些时候grep很管用(很方便)
在powershell中,可以通过安装第三方工具实现grep(当然用法于Linux上的有所区别
(可以通过scoop搜索相关工具并安装)

PS C:\Users\cxxu_11> scoop search grep
'main' bucket:
    busybox (4487-gd239d2d52) --> includes 'egrep'
    gettext (0.21-v1.16) --> includes 'msggrep.exe'
    gow (0.8.0) --> includes 'egrep.exe'
    grep (2.5.4)
    nim (1.6.0) --> includes 'nimgrep.exe'
    pcregrep (10.20)
    ripgrep (13.0.0)
    rktools2k3 (1.0) --> includes 'qgrep.exe'
    ugrep (3.3.8)
    unxutils (2007.03.01) --> includes 'agrep.exe'

例如,可以安装第一个工具箱,内部就包含有grep工具

PS C:\Users\cxxu_11> printenv | grep apps -i

CPwindowsApps=C:\Program Files\windowsApps
oneNote10Home=C:\Program Files\windowsApps\Microsoft.Office.OneNote_16001.14326.20458.0_x64__8wekyb3d8bbwe
Path=D:\Program Files\PowerShell\7;C:\Program Files\WindowsApps\Microsoft.WindowsTerminal_1.9.1942.0_x64__8wekyb3d8bbwe;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xuchaoxin1375/article/details/121221198
今日推荐