How to delete only first ocurrence of line without "}" in Powershell?

Yuri 21 Reputation points
2021-01-27T18:54:33.793+00:00

Hi Everyone!   
  
I try to delete carriage return in a file, all lines have a "}" at end, but some lines, have a return carriage and the "}" its down.... Here an example of my code and file, but doesn't work. Thank You Very Much!!  
  
Code:  
  
$InputFile='Original.txt'  
$OutPutFile='NewFile.txt'  
(Get-Content $InputFile) | ForEach-Object -Begin {  
$results = @()
} -Process {  
$out = $_.Split("}").GetUpperBound(0)  
[int
    ]$iOut = [int
    ]$out   
if($iOut -lt 1){$_.Replace("`r`n",
        "")
    }else{$_
    }
} | Set-Content $OutPutFile  
  
File: 33|1|||19|2020-04-22|76515729|CARMONA Y CARMONA|||0|0|0||0|
}  
33|2|||19|2020-04-22|76519254|COMERCIALIZADORA CECINAS LA ALEMANA LTDA|||0|0|0||0|
}  
33|3|||19|2020-04-22|76524673|SOCIEDAD GANADERA NEVADA LTDA|||0|0|0||0|
}  
33|4|||19|2020-04-22|76524692|GESTION DE PROYECTOS INGENIERIA Y CONSTR|||0|1|1||1|
}  
33|5|||19|2020-04-22|76524805|SOCIEDAD DE SERVICIOS RD RENTAL LIMITADA|||0|1|1||0|
}  
33|6|||19|2020-04-22|76526632|CENTRO DE DIAGNOSTICO Y RESONANCIA SPA|||2|2|2||2|
}  
33|7|||19|2020-04-22|76536309|UNISERVICE SPA|||4|4|4||4|
}  
33|8|||19|2020-04-22|76536461|AGRICOLA,FORESTAL E INMOBILIARIA BOSQUE |||0|3|3||0|
}  
33|6313350|||19|2020-04-22|76539612|INGENIERIA Y SERVICIOS LOS NOGALES SPA   
|||0|1|0||0|
}  
33|9|||19|2020-04-22|76550378|TRANSPORTES LAYANA Y CABRERA LIMITADA|||0|5|5||5|
}  
33|10|||19|2020-04-22|76555900|AGRICOLA DECOTERRA LIMITADA|||0|5|5||5|
}  
33|11|||19|2020-04-22|76556837|TECMA ASCENSORES LTDA.|||0|2|2||2|
}  
33|12|||19|2020-04-22|76562934|CORREO PRIV. TEF Y RECAUD. LORENA DONOSO|||0|4|4||4|
} 
33|13|||19|2020-04-22|76570460|RESGUARDO Y SEGURIDAD LTDA|||0|0|0||0|
}```
Windows Server 2012
Windows Server 2012
A Microsoft server operating system that supports enterprise-level management, data storage, applications, and communications.
1,618 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,624 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,596 Reputation points
    2021-01-27T21:18:26.887+00:00

    This is a little clumsy (maybe someone will write a zero-length negative lookahead regex that's more elegant!), but it works:

    $in = Get-Content c:\junk\yuri.txt -raw
    $in = $in -replace "`r`n", '' -replace "}$",""  # remove all Cr/Lf AND the last "}"
    $out = $in -split '}'
    $out |
        ForEach-Object{
            "$_}" | Add-Content c:\junk\yuri1.txt
        }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.