1

I need to replace the ' ",' to '",'. I need to remove all spaces but not the space in Abc CDE. In the end of each line there's also a space that I want to remove.

I was trying powershell but this doesn't work:

set "search=' ",'" set "replace='",'" :DoReplace echo ^(Get-Content "aaa.TXT"^) ^| ForEach-Object { $_ -replace "%search%", "%replace%" } ^| Set-Content "New.txt">Rep.ps1 Powershell.exe -executionpolicy ByPass -File Rep.ps1 

This is the example I use:

"0","46","","","","Abc CDE ApS ","","","","","81602832 ","","","","","","","","","","" "0","46","","","","Abc CDE ApS ","","","","","81602832 ","","","","","","","","","","" "0","46","","","","John Stewart ","","","","","85505637 ","","","","","","","","","","" 

2 Answers 2

2

In PowerShell you could use a regular expression replacement like this:

(Get-Content 'aaa.txt') -replace ' +(",)','$1' | Set-Content 'aaa.txt' 

but a much cleaner approach would be to import the file as a CSV and trim trailing spaces from each column, since the file seems to be a CSV anyway:

$csv = 'aaa.txt' $outfile = 'bbb.txt' # generate artificial headers $cnt = @((Get-Content $csv -TotalCount 1) -split '","').Count $headers = 1..$cnt | ForEach-Object { [string][char]($_+64) } Import-Csv $csv -Header $headers | ForEach-Object { foreach ($prop in $_.PSObject.Properties) { $_.$($prop.Name) = $prop.Value.Trim() } $_ } | ConvertTo-Csv -NoType | Select-Object -Skip 1 | Out-File $outfile 

With that said, for a batch approach (you should have mentioned that in your question) it would usually be much simpler to just use a Windows version of sed:

sed -i "s/ \+\",/\",/g" aaa.txt 
Sign up to request clarification or add additional context in comments.

Comments

0

Is it that what you want?

$a = '"0","46","","","","Abc CDE ApS ","","","","","81602832 ","","","","","","","","","","" ' # delete all ' "' While($a -like '* "*') { $a = $a -replace ' "', '"' } # delete last character $a = $a.subString(0,$a.length-1) # whrite it into the console echo "" echo $a 

2 Comments

Thanks @IkeRoyle It works but I need this in a batch file and I don't know how to do it. Can you please help me? I have a txt file with multiple lines and I want to copy to a new file without the spaces.
Well, can your read Batch-files? If yes, you could run the powershell-file with your batch -> take the info from your txt-file, edit it and whrite it into another txt-file -> then read this with a Batch-file // right now I don't have the time to bring you a new solution. If my awser above helped you, you could mark it as awnser. Good luck!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.