Select-字符串输出给出整个对象,不响应过滤器
Select-String output gives whole object and does not respond to filter
我在 Unix/Linux 上做过一些编程,但最近 PowerShell 在我的工作中更有用。但是 grep
的缺失让我发疯。谁能解释一下这个现象?
我正在尝试让今天的 IOTA 值恢复到我可以 Write-Output
.
的值
$webpage = Invoke-WebRequest -Uri https://coinmarketcap.com/currencies/iota/
$filtered = $webpage.ParsedHtml.body.innerText
这提供了我需要的所有信息,但需要更多过滤。
$filtered | Select-String "IOTA"
这给了我 $filtered
中的所有信息,但不知何故它无法 select 该字符串。但是,如果我这样做,它会起作用:
$filtered > filter.txt
Get-Content filter.txt | Select-String "IOTA"
我一直在尝试在网上搜索答案,我发现最接近的是它可能将 $filtered
作为对象处理,而不是字符串。如果我检查,它说它是一个字符串。
PS> $filtered.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
这不是我第一次经历 Select-String
失败。我做错了什么?
而不是$filtered | select-string "IOTA"
您可以使用 ($filtered | select-string ".*IOTA.*")
然后您可以选择匹配结果。在这种情况下使用正则表达式总是好的。但我建议您首先用换行符拆分 $filtered 。这将消除混淆。那就继续吧。
所以这对您来说应该是更好的选择:
(($filtered).Split('`n') | select-string ".*IOTA.*")
$filtered
是一大串。如果将 $filtered
保存到文件,然后使用 get-content 读取它,Get-Content cmdlet 将 return 文件内容作为由换行符分隔的字符串数组。
但是,由于 html 是结构化的,我建议直接获取您感兴趣的元素,如下所示:
$webpage.parsedhtml.getElementById("quote_price").innerText
returns:
3.45 USD
我在 Unix/Linux 上做过一些编程,但最近 PowerShell 在我的工作中更有用。但是 grep
的缺失让我发疯。谁能解释一下这个现象?
我正在尝试让今天的 IOTA 值恢复到我可以 Write-Output
.
$webpage = Invoke-WebRequest -Uri https://coinmarketcap.com/currencies/iota/
$filtered = $webpage.ParsedHtml.body.innerText
这提供了我需要的所有信息,但需要更多过滤。
$filtered | Select-String "IOTA"
这给了我 $filtered
中的所有信息,但不知何故它无法 select 该字符串。但是,如果我这样做,它会起作用:
$filtered > filter.txt
Get-Content filter.txt | Select-String "IOTA"
我一直在尝试在网上搜索答案,我发现最接近的是它可能将 $filtered
作为对象处理,而不是字符串。如果我检查,它说它是一个字符串。
PS> $filtered.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object
这不是我第一次经历 Select-String
失败。我做错了什么?
而不是$filtered | select-string "IOTA"
您可以使用 ($filtered | select-string ".*IOTA.*")
然后您可以选择匹配结果。在这种情况下使用正则表达式总是好的。但我建议您首先用换行符拆分 $filtered 。这将消除混淆。那就继续吧。
所以这对您来说应该是更好的选择:
(($filtered).Split('`n') | select-string ".*IOTA.*")
$filtered
是一大串。如果将 $filtered
保存到文件,然后使用 get-content 读取它,Get-Content cmdlet 将 return 文件内容作为由换行符分隔的字符串数组。
但是,由于 html 是结构化的,我建议直接获取您感兴趣的元素,如下所示:
$webpage.parsedhtml.getElementById("quote_price").innerText
returns:
3.45 USD