0

I'm looking to parse out a specific blob of text to a different file. However, I'm having an issue with the .IndexOf() method when trying to define my endIndex.

The string I'm looking for will start with the phrase:

/* original invocation */

It will then list the original invocation contained in the same type of comment:

/* 
~~~ 
/"

I just finished "Learn PowerShell in a Month of Lunches" so I'm still fresh. But, my thought process is this:

  1. Get the starting line number of "/* original invocation */" and use that as a startIndex
  2. Get where-ever the last "*/" is after that and make that line the endIndex.
  3. Use those values to parse the blob of text I'm after to a file. I assume I could use something like SubString() and pipe it to Out-File.

Here is my script so far:

$file = Get-Content ("file/to/be/read")
$searchString = "/* original invocation */"
$startIndex = $file.IndexOf($searchString)
# Lets say the value of $startIndex is 18676

$endIndex = $file.IndexOf('\*/',$startIndex)
# The error.  int IndexOf(string value, int startIndex) 
# MethodException: Cannot find an overload for "IndexOf" and the argument count: "2".

I'm pretty lost. If this is easier in Python, please let me know.

The issue I have is when trying to define the endIndex. I get the following error:

MethodException: Cannot find an overload for "IndexOf" and the argument count: "2".

I looked at the overloads for IndexOf and I seem to be matching it, so I must really be missing something. It doesn't seem to matter how many arguments I put in. I also noticed if I just do a random string, I get results of "-1".

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    You are actually calling `Array.IndexOf()` because `Get-Content` returns an array of lines by default. Use `Get-Content "file/to/be/read" -Raw` to let it output a single, multiline string. – zett42 Jan 27 '23 at 18:27
  • If you're actually looking the array's IndexOf and not for string's IndexOf, then the array instance method IndexOf only has one overload, you're probably looking for the static IndexOf (`[array]::IndexOf(Array, Object, Int32)`) – Santiago Squarzon Jan 27 '23 at 18:29
  • IndexOf does not work with Arrays. You need a list object. See my solution from earlier this week : https://stackoverflow.com/questions/75232037/powershell-continue-where-i-left-off-in-a-loop/75236889#comment132778609_75236889 – jdweng Jan 27 '23 at 18:32
  • @jdweng It does: `('foo', 'bar').IndexOf('bar')` prints `1` as expected. But there is only one overload that can be called as an instance method (inherited by `Array` from [`IList`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.ilist.indexof?view=net-7.0)). Other overloads are only available as [static `Array` methods](https://learn.microsoft.com/en-us/dotnet/api/system.array.indexof?view=net-7.0). – zett42 Jan 27 '23 at 18:52
  • As a solution to your issue, you might have a look here [How do I find the position of substring in PowerShell after position x?](https://stackoverflow.com/q/31663644/1701026) – iRon Jan 28 '23 at 09:16

0 Answers0