I will write about how to grep like Linux in PowerShell on Windows.
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]#
command
<command> | Out-String -Stream | Select-String <The character string you want to search>
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>
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>
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