【问题标题】:Is there a way to display the lines of text that meets a condition with PowerShell有没有办法用PowerShell显示符合条件的文本行
【发布时间】:2019-06-06 19:57:04
【问题描述】:
$data = Select-String -Path $selectedDirectory\$sqlFile -Pattern "GRANT" -Context 5,5

我想使用 PowerShell 读取 .SQL 文件,并且我们希望确保用户在没有人工检查文件以查看文件是否正常的情况下使用 GRANT、DROP 或 DELETE。

我的 1 行仅查看 GRANT,但我认为它不起作用。

如果关键字在文件中,我想在屏幕上显示部分文本 +/- 5 行发现有问题的文本。

有没有办法更改具有违规搜索条件的特定行的文本颜色(所有其他行将默认显示)

【问题讨论】:

  • 对于初学者,"GRANT" 应该放在引号中以表示字符串。
  • 后来我意识到了。
  • Select-String-Pattern 参数使用正则表达式。您可以使用-Pattern "GRANT|DROP|DELETE" 找到所有这些关键字的出现
  • Write-Host 可以使用颜色输出,但通常不推荐,因为它无法重定向。
  • 我认为 OP 只担心交互输出到终端,所以重定向不是问题。

标签: powershell select-string


【解决方案1】:

如果你想在控制台上显示颜色,你需要使用Write-Host

$data = Select-String -Path $selectedDirectory\$sqlFile -Pattern "GRANT|DROP|DELETE" -Context 5,5
$data | Foreach-Object {
    $_.Context.Precontext
    Write-Host $_.Line -ForeGroundColor Cyan
    $_.Context.Postcontext
}

【讨论】:

    【解决方案2】:

    我会试一试的。

    此函数获取一个文件,搜索这些关键字,然后打印 +/- 5 行。这很容易,我相信您知道它是如何工作的以及如何修改它。您可以找到matchinfo 类的引用(由 Select-String 返回(here

    Function Get-SQLForbiddenWords ($sqlDataFile) {
        $data =  Select-String -Path $sqlDataFile -Pattern "GRANT|DROP|DELETE" 
        Foreach ( $line in $data) {
            $lineNumberS = $line.LineNumber - 5
            $lineNumberE = $line.LineNumber + 5
            echo ('Bad Command Detected: {0}' -f $line.line)
            (Get-Content $sqlDataFile)[$lineNumberS..$lineNumberE]
            echo "`n"
        }
    }
    

    这很有趣。输出:

    Bad Command Detected: DROP
    this is the sixth line 
    GRANT
    this is the seventh line
    this is the eighth line
    DROP
    this is the ninth line 
    this is the tenth linet
    this is the eleventh line 
    this is the twelfbthfbth line 
    

    【讨论】:

    • 虽然这可能可行,但Select-String 已经提供了-Context 选项来获取前后行。 @AdminOfThings 的答案利用它以更少的努力产生类似的输出
    【解决方案3】:

    对于初学者,"GRANT" 应该用引号括起来以表示字符串。

    如果您注意到,$line = Select-String -Pattern "Grant" 将返回一个对象。

    如果使用Get-Member查看对象的属性,其中之一是LineNumber

    如果您使用$data = Get-Content File.sql 或任何类似的方法读取了文件的内容,您的数据将作为一个数组对象。现在你可以使用这个行号来提取+/- 5 行,就像$data[50..60] 一样。这将显示第 50 到第 60 行的输出行。您可以轻松地将 50 和 60 替换为您的变量。

    【讨论】:

    • 但是-Context 5,5 参数已经处理了前后行
    【解决方案4】:

    另一种方法是使用oss函数(=Out-String -Stream)。

    Select-String "\b(GRANT|DROP|DELETE)\b" .\test.txt -Context 5 | oss | foreach { Write-Host $_ -ForegroundColor ("White","Cyan")[$_[0] -eq '>'] }
    

    以下内容可能会使其更具可读性。

    Select-String "\b(GRANT|DROP|DELETE)\b" .\test.txt -Context 5 | Out-String -Stream | foreach {
        if(-not $_) { return }
        $fileName,$lineNumber,$line = $_.Split(":", 3)
        $color = if($_.StartsWith(">")) { "Cyan" } else { "White" }
        Write-Host $fileName $lineNumber.PadLeft(3, "0") $line -ForegroundColor $color -Separator "  "
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多