0

INPUT FILE

Hi Everyone,

I am trying to get the list of approver for each group. Below code is an example of some coding I am trying to do.

Expected Output

EXPECTED OUTPUT

Any clues how to do it?

Thank you

$file = 'textfile.txt'
$groups

foreach($line in Get-Content $file){

$Lines = Get-Content $file
$Output = 

For( $i = 0; $i -lt $Lines.Length; ++$i)
{
    If($Line.StartsWith("# APPROVER:"))
    {
        $Lines[$i]
        # Need to ignore lines in between
        :LookForGroup Do
        {
            ++$i
            If( $Lines[$i].StartsWith($groups))
            {
                $Lines[$i]
                ""
                Break LookForGroup
            }
        } While($true)
    }
}
}
$Output
4
  • Please do not add text as links to images, but instead insert these into the question as formatted text. Commented Feb 13, 2020 at 19:22
  • Sure, do you got solution Theo? Commented Feb 13, 2020 at 19:46
  • The input is a mess. How would you decide what a valid groupname would be and what not? Commented Feb 14, 2020 at 14:01
  • Hi Theo, Input has been edited. Commented Feb 14, 2020 at 14:42

1 Answer 1

1

It looks like you're splitting and replacing it to get the file content into individual lines. However, Get-Content does that by default. Moreover, in your code $line & $content would have the same values.

I'm giving this a shot but I think what you are trying to do is look forward through an array of lines to find the group name that may come at some arbitrary point after you spot the "approver".

I manually typed out a file as a pretty good match to what you demonstrated in the revised question. Came up with the below. Again this is off the top of my head, but it seems to work as you dictated above.

Note: I added the $Groups array just to simulate what you said in your last comment.

$File = 'C:\Temp\InputTest.txt'
$Groups = @( 'Group1','Group2','Group3','Group4','Group5' )

$Lines = Get-Content $File
$Output = 
For( $i = 0; $i -lt $Lines.Length; ++$i )
{
    If( $Lines[$i] -match '^# approver' )
    {
        $Approver = $Lines[$i].split(':').Trim()[1]
        # Need to ignore lines in between
        :LookForGroup Do
        {
            ++$i
            ForEach ( $Group in $Groups )
            {
                If( $Lines[$i] -match "^$Group" )
                {
                    $Approver + ' ' + $Lines[$i]
                    ++$i
                }
                ElseIf( $Lines[$i]  -match "(^$|\r\n|^######)")
                {# Looking for a signal to move on...
                    Remove-Variable Approver #Maybe don't need this???
                    Break LookForGroup
                }
            }
        } While($true)
    }
}
#This will display the output, you can do something else with it...
$Output

What I'm doing is using $i kind of like a cursor. Once an approver line is spotted I go through each group to see if the line matches the group name.If yes I output the approver and the group name as you specified. I then increment $i thus moving one line forward in the file. If the line doesn't match the group I check for some characteristics to see if we've reached the end of a section (as defined by your example). If yes I'll break the Do loop which will cause the For loop to continue, note: that $i will remain as we set it...

I admit this is a little kludge let me know...

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Steve, Thank you for your good effort. I see that you really spent good time on it. Unfortunately, the above code gives me no solution, I am not sure why. I will re-edit my code above again with some additional images and re-share again. Please be informed that I have already save my group names in a $group. Therefore, I would like to use Startswith operator rather than match/eq. Kindly take a look to the above code and suggest. Thank you
I revised the answer to match your description better... Let me know ... Note: The reason I used -match was because .StartsWith() is case sensitive, to as apposed to normalizing with something like .ToLower()...
Hi Steven, Thank you for your effort. The code seems to be working fine. The only problem I am getting is that there are more groups than the result I am getting. Other than this, the data and the code is correct. Please let me know if we can make any improvements.
Without the real input file it'd be hard for me to take this further. Like I said this exercise was really off the top of my head. I'd imagine if the file isn't perfect... Perhaps there are some groups that don't have approvers; Unless you can rely on the pattern at some point the logic will fail. In an unstructured case like this you'd really have to know your input and make code adjustments to each exception you find. Let me know if there's anything else I can do. Thanks.
The intent is to look for a signal that I don't need to keep looking for groups. Again being partly inhibited by not having the real input; the Regular expression is meant to match a blank line or a whole bunch of hash signs (since the approver lines also started with a hash).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.