[PowerShell] How to search for rows like Linux grep (Windows)

Introduction

I will write about how to grep like Linux in PowerShell on Windows.

What is grep in the first place?

Simply put, it's a command that searches a line in a Linux file. Please see the following page for details.

Detailed summary of grep command [Linux command collection]

Example of use


[root@tspweb01 network-scripts]# grep IPADDR ifcfg-enp0s8
IPADDR=192.168.56.30
[root@tspweb01 network-scripts]# 

** * State before command execution **

Before command execution


[root@tspweb01 network-scripts]# cat ifcfg-enp0s8
TYPE="Ethernet"
BOOTPROTO="none"
IPV6INIT="no"
NAME="enp0s8"
UUID="81d71b89-d1b6-4ca9-853d-c5bf74c8487e"
DEVICE="enp0s8"
ONBOOT="yes"
IPADDR=192.168.56.30
PREFIX=24
[root@tspweb01 network-scripts]# 

PowerShell grep method

Execution command

command


 <command> | Out-String -Stream | Select-String <The character string you want to search> 

Usage example ①

Extract the "Domain" line from the execution result of ** Get-NetFirewallProfile **

Before character string extraction


 PS C:\Users\Administrator> Get-NetFirewallProfile 

Name                            : Domain
Enabled                         : False
DefaultInboundAction            : NotConfigured
DefaultOutboundAction           : NotConfigured
AllowInboundRules               : NotConfigured
AllowLocalFirewallRules         : NotConfigured
AllowLocalIPsecRules            : NotConfigured
AllowUserApps                   : NotConfigured
AllowUserPorts                  : NotConfigured
AllowUnicastResponseToMulticast : NotConfigured
NotifyOnListen                  : False
EnableStealthModeForIPsec       : NotConfigured
LogFileName                     : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
LogMaxSizeKilobytes             : 4096
LogAllowed                      : False
LogBlocked                      : False
LogIgnored                      : NotConfigured
DisabledInterfaceAliases        : {NotConfigured}

~~~abridgement~~~

PS C:\Users\Administrator>  

** Character string extraction result **

Before character string extraction


PS C:\Users\Administrator> Get-NetFirewallProfile | Out-String -Stream | Select-String Domain

Name                            : Domain

PS C:\Users\Administrator>  

Usage example ②

Extract the "test" line from ** test.txt **

Before character string extraction


 PS C:\Users\Administrator\Desktop> cat .\test.txt
aaaa

aaaa
test


vagfafa

fafeaea

iiaaaa

faejfajefa

** Character string extraction result **

Character string extraction result


PS C:\Users\Administrator\Desktop> cat .\test.txt | Out-String -Stream | Select-String "test"

test

PS C:\Users\Administrator\Desktop>  

Summary

You can search for a line with the following command.

command


 <command> | Out-String -Stream | Select-String <The character string you want to search> 

Recommended Posts

[PowerShell] How to search for rows like Linux grep (Windows)
How to install Windows Subsystem For Linux
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
How to use the Linux grep command
How to create a shortcut command for LINUX
How to add Anaconda Powershell Prompt to Windows Terminal?
How to create a local repository for Linux OS
[Windows] RDP to Windows via Linux
How to use search sorted
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
[For beginners] How to use for statements on Linux (variables, etc.)
How to implement 100 data science knocks for data science beginners (for windows10 Home)
How to implement Python EXE for Windows in Docker container
How to get the printer driver for Oki Mac into Linux
[Unity] How to run ML-Agents Release 8 even for transcendental beginners [Windows]
[For non-programmers] How to walk Kaggle
How to use Dataiku on Windows
How to install wkhtmltopdf (Amazon Linux2)
How to install VMware-Tools on Linux
How to deploy django-compressor on Windows
How to install MBDyn (Linux Ubuntu)
How to use bing search api
How to check Linux OS version
[Windows] Transfer event logs to Linux
pykintone on Windows Subsystem for Linux
How to install music 21 on windows
Verification of how to periodically execute a script on a Linux server on Windows
How to build my own Linux server
Windows Subsystem for Linux is not displayed
How to set optuna (how to write search space)
How to update to Chainer 2.0 (Windows + CUDA 8.0 + CUDNN)
[Kivy] How to install Kivy on Windows [Python]
I want to know how LINUX works!
Windows → linux Tips for bringing in data
How to use Cmder with PyCharm (Windows)
[Linux] How to use the echo command
How to update php on Amazon linux 2
How to use Google Assistant on Windows 10
WSL2 (Windows Subsystem for Linux) installation procedure
How to display emoji on Manjaro Linux
How to search Google Drive with Google Colaboratory
[Python] Organizing how to use for statements
How to install packages on Alpine Linux
How to install Anisble on Amazon Linux 2
How to operate Linux from the console
How to install richzhang / colorization on Windows 10
How to update security on CentOS Linux 8
How to use Pylint for PyQt5 apps
How to install php7.4 on Linux (Ubuntu)
How to make multi-boot USB (Windows 10 compatible)
How to use "deque" for Python data
How to find large files on Linux
How to use fingerprint authentication for KDE
How to get started with laravel (Linux)
Useful for changing permissions on Linux! How to count up to 31 with one hand.
[linux] How to quit without waiting for the other party to disconnect with telnet