2

I have done some programming on Unix/Linux, but lately PowerShell has been more useful in my work. However the lack of grep drives me mad. Can someone explain this phenomenon?

I am trying to get the value of IOTA for today returned to a value that I can Write-Output.

$webpage = Invoke-WebRequest -Uri https://coinmarketcap.com/currencies/iota/
$filtered = $webpage.ParsedHtml.body.innerText

This gives me all the information I need, but it needs more filtering.

$filtered | Select-String "IOTA"

This gives me all the information in $filtered, somehow it does not manage to select that string. However, if I do this it works:

$filtered > filter.txt
Get-Content filter.txt | Select-String "IOTA"

I have been trying to search the web for answers, the closest thing I found is that it maybe processes $filtered as an object, not a string. If I check, it says it is a string.

PS> $filtered.GetType()

IsPublic IsSerial Name                     BaseType
-------- -------- ----                     --------
True     True     String                   System.Object

This is not my first experience of Select-String failure. What am I doing wrong?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I can't answer your question, but I can sympathize. With near 20 years of Boerne and bash, trying to make powershell do anything is an effort in futility.... My solution -- install Msys, make sure to set your path, and grep away... – David C. Rankin Dec 28 '17 at 06:50
  • I must be sick, I do this for fun... And to be fair, I do enjoy powershell, the ability to have a `Microsoft.Powershell_profile.ps1` is a real improvement... – David C. Rankin Dec 28 '17 at 07:02
  • @DavidC.Rankin Nice to know I am not the only one ripping my hair off... – CyberEngineer Dec 28 '17 at 07:28
  • 1
    It's not as much that you are doing anything wrong but you are struggling with the object nature and return types from different commands. If you inspect the `$filtered` object you'll find that it's a string. Important to note is that it's *one* string of x length. Your `select-string` finds `IOTA` in that one string and returns the string. By using `get-content`, you are reading the file line by line and pass each line to `select-string`. `select-string` again only shows the string matching `IOTA` – Lieven Keersmaekers Dec 28 '17 at 08:13
  • 2
    Instead of outputting it to a file, you can either use `$filtered -split '``n' | select-string "IOTA"` or use a regex `($filtered | select-string ".*IOTA.*").Matches.Value` – Lieven Keersmaekers Dec 28 '17 at 08:21
  • @LievenKeersmaekers When you need 2 comments to respond it's probably the right time for writing an answer instead. ;) – Ansgar Wiechers Dec 28 '17 at 10:31
  • @AnsgarWiechers - That was just for OP to pass the hurdle but I was waiting for a more detailed and comprehensive answer from you :) – Lieven Keersmaekers Dec 28 '17 at 10:43

2 Answers2

2

$filtered is one big string. If you save $filtered to a file and then read it using get-content, the Get-Content cmdlet will return the contents of the file as an array of strings delimited by newline characters.

However, since html is structured, I'd recommend to fetch the element you're interested in directly, like this: $webpage.parsedhtml.getElementById("quote_price").innerText

returns:

3.45 USD

jon Z
  • 15,838
  • 1
  • 33
  • 35
1

Instead of $filtered | select-string "IOTA"

You can Use ($filtered | select-string ".*IOTA.*")

And then you can choose for the matching result. It is always good to use regex in this case. But I would recommend you to split the $filtered first with newlines. That will remove the obfuscation. Then proceed with it.

So this should be a better option for you:

(($filtered).Split('`n') | select-string ".*IOTA.*")
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45