Reputation: 55
In the text file List.txt
I want to delete the quotation marks and save in ListF.txt
I can run the following code in the powershell console:
(Get-Content List.txt) | ForEach-Object { $_ -replace '"','' } | Set-Content ListF.txt
However I can't run the same command with cmd:
powershell.exe -command "{(Get-Content List.txt) | ForEach-Object { $_ -replace '"','' } | Set-Content ListF.txt}"
I am very grateful for all the help!
Upvotes: 0
Views: 141
Reputation: 27418
Look Ma, no quotes or pipes. "set-content path value".
powershell set-content listf.txt ((get-content list.txt) -replace [char]34)
[char]34 is doublequote:
[int][char]'"'
34
Upvotes: 1
Reputation: 1169
You need to escape some special characters as this comment says.
This is how cmd.exe works, but I cannot explain why.
powershell.exe -command "(Get-Content List.txt) | ForEach-Object { $_ -replace '""','' } | Set-Content ListF.txt"
powershell.exe -command "(Get-Content List.txt) | ForEach-Object { $_ -replace '\"', '' } ^| Set-Content ListF.txt"
about |
escaping
:: escape `|` until `"` has its pair
echo "1 | 2 " ^| 3 ^| " 4 | 5"
:: every `|` needs escaping because `"` does not have its pair
echo "1 | 2 " ^| 3 ^| 4 ^| 5"
:: no escape
echo "1 | 2 "" | 3 | 4 | 5"
powershell.exe -command (Get-Content List.txt) ^| ForEach-Object { $_ -replace '\"', '' } | Set-Content ListF.txt
powershell.exe -command (Get-Content List.txt) ^| ForEach-Object { $_ -replace '""""', '' } ^| Set-Content ListF.txt
Upvotes: 1