Home > Back-end >  Replace string in an imported CSV file / multiple conditions
Replace string in an imported CSV file / multiple conditions

Time:03-15

I need to import a CSV file and then replace the string domain\username AND the string domain\login with the word SSO.

I know that replacing only domain\username would be:

(Get-Content $ImportCPUFile) |
    ForEach-Object { $_ -replace '"domain\username"', '"sso"' } |
    Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii

But how to make an OR statement in the ForEach-Object function for domain\username and domain\login? I tried:

(Get-Content $ImportCPUFile) |
    ForEach-Object {$_ -replace '"domain\username"', '"sso"' -or $_ -replace '"domain\login"', '"sso"'} |
    Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii

But it returns only TRUE statements.

CodePudding user response:

Try RegEx or '|'

(Get-Content $ImportCPUFile) |
ForEach-Object { $_ -replace '"domain\\username"|"domain\\login"', '"sso"' } |
Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii
  • Related