Windows Server 2012
A Microsoft server operating system that supports enterprise-level management, data storage, applications, and communications.
1,618 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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|
}```
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
}