PowerShell regex to match file content and output to screen (or save to file)

code:

foreach ($line in Get-Content -path .\test.sql) {
    
     if ($line -match 'bdw_\w*.\w*') {
    
    write-output $matches[0]}}

Ideas:

  1. Read the file and iterate through
    foreach ($line in Get-Content -path .\test.sql)

  2. Regular matching
    if ($line -match 'bdw_\w*.\w*')
    This match will output True or False, you need to use $matches output

  3. output
    {write-output $matches[0]}}

result

Tips1:
If necessary, you can flexibly modify the regular matching method.
Modify this -> 'bdw_\w*.\w*'

Tips2:
If you need to output to a file, add > filename.txt at the end

Guess you like

Origin blog.csdn.net/qq_44491709/article/details/125305868