Deleting one line if it comes up Successively in a text file

Hi ,

I have a text a file with below kind of content. If we see the content the line which starting with the word “displayName”: came Successively (one after another) in some places of this content, not in all places.

I want to delete one line out of two lines if it Successively comes up only. After deleting no space also should be there in the place of deleted line. i searched in google to get this snippet in PowerShell, but no luck… please help.

“buildNumber”: “20210208.2”,
“result”: “succeeded”,
“name”: “LDAP”,
“displayName”: “Marks kim”,
“buildNumber”: “20210208.1”,
“result”: “succeeded”,
“name”: “BESS”,
“displayName”: “Phils.john”,
“displayName”: “Phils.john”,
“buildNumber”: “222290”,
“result”: “succeeded”,
“name”: “ELTS”,
“displayName”: “Phils.john”,
“buildNumber”: “222168”,
“result”: “succeeded”,
“name”: “BESS”,
“displayName”: “Phils.john”,
“displayName”: “Phils.john”,
“buildNumber”: “221393”,
“result”: “succeeded”,
“name”: “MKIS”,
“displayName”: “Phils.john”,
“buildNumber”: “222298”,
“result”: “succeeded”,
“name”: “ELTS”,
“displayName”: “Phils.john”,

What have you tried so far? Please show your code.

Looks like 2 possible solutions to me. Use regex with the -replace operator or iterate over the lines in a foreach loop. I’d probably recommend using regex with the -replace operator. Here’s some references to get you started.
Get-Help about_comparison_operators (see replacement operator)
Get-Help about_regular_expressions

Post back with some code or questions.

You can use a simple loop to compare the current line with the next line. Example:

$text = Get-Content .\text.txt

$newText = for ($i = 0; $i -lt $text.Count; $i++) {

    if ($text[$i] -ne $text[$i+1]) {

        $text[$i]

    }

}

Set-Content -Path E:\Temp\Files\newFile.txt -Value $newText