Using Windows powershell, how can I delete all strings directly in between ";" or "," and "false"?

Viewed 53

I have

598842,3605false;3605true;36054true;36054true;360true;360false;36054true
598842, 3605true;36054true;36054true;360true;360false;36054true

I want to turn it into

598842, 3605true;36054true;36054true;360true;36054true
598842, 3605true;36054true;36054true;360true;36054true

Pathetic attempts below.

(Get-Content -Raw .\actswithpeople.csv)  -Replace '.*?(\"totalCount\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv)  -Replace '.*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv)  -Replace '\",\".*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv)  -Replace '.*?(\"false;\":)', '' | Out-File -FilePath actpageline.txt -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s); \[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s);\[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s);\[false]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), ',(?s)\"[false]\"', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s): \[.+?\r?\n        \]', { param($match) $match.Value -replace '[\r\n\"]' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)\[.+?\r?\n\]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;\[.+?\r?\n\]', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?\r?\n\', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?\r?\n', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    [regex]::Replace((Get-Content -Raw .\actswithpeople.csv), '(?s)false;.+?', { param($match) $match.Value -replace '' }) | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?s)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo2.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.s)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?,)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?.)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=,.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=,.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII;(Get-Content -Raw .\noo.csv) -replace 'true','' | Out-File noo2.csv;
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=,.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII;(Get-Content -Raw .\noo.csv) -replace 'true','' | Out-File noo2.csv;(Get-Content -Raw .\actswithpeople.csv) -replace 'true','' | Out-File actsnotrue.csv
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=;.*?)[\r\n"](?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=true;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.+?)(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.+?)(?<=,.+?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<=,.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?<=,.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<=;.*?)(?=.*?false)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<=;.*?)(?=false)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.)(?<=;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.?)(?<=;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(.*?)(?<=;)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '.(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII
    (Get-Content -Raw .\actswithpeople.csv) -replace '.*?(?<=;.*?)(?=.*?false;)' | Out-File -FilePath noo.csv -Force -Encoding ASCII

I didn't save the output of every one. I thought this was a simple one I could crack, but 3 hours later, I was wrong, it isn't. Please help.

2 Answers

Why not just this:

'
598842,3605false;3605true;36054true;36054true;360true;360false;36054true
598842, 3605true;36054true;36054true;360true;360false;36054true
' -replace '\d+false;' -replace ', |,', ', '

# Results
<#
598842, 3605true;36054true;36054true;360true;36054true
598842, 3605true;36054true;36054true;360true;36054true
#>

A generalized solution that also handles removal of trailing ...false fields correctly.

# Note the trailing ";99999false"
$str = @'
598842,3605false;3605true;36054true;36054true;360true;360false;36054true;99999false
598842, 3605true;36054true;36054true;360true;360false;36054true
'@

# Result matches the expected output in the question.
$str  -replace '[,;] ?\d+false' -replace '(?m)(\d+)[,;] ?', '$1, '
  • The first -replace operation removes all ...false fields including their preceding separator (, or ;, optionally followed by a space).

  • The second -replace ensures that the first separator is a ,, followed by a space.

  • Inline regex option (?m) (the multi-line option, which makes ^ and $ match the start and end of each line) is only necessary if the input is a single, multi-line string rather than an array of strings.

Related