PowerShell正则表达式匹配文件内容并输出到屏幕(或保存到文件)

代码:

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

思路:

  1. 读取文件并遍历
    foreach ($line in Get-Content -path .\test.sql)

  2. 正则匹配
    if ($line -match ‘bdw_\w*.\w*’)
    这个匹配会输出True或False,需要使用$matches输出

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

result

Tips1:
如果有需要,灵活修改正则匹配的方式即可。
修改这个 -> ‘bdw_\w*.\w*’

Tips2:
如果需要输出到文件,结尾添加 > filename.txt

猜你喜欢

转载自blog.csdn.net/qq_44491709/article/details/125305868