Page 1 of 1

Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 04:30
by afastboi
Hello !

I have this problem :

I have a text file with lines that look like this :
"300040082800016351540;01;EUR;310719; 84,67-;"
"300040082800016351540;07;EUR;010819; 84,67-;"
"300040082800016356293;01;EUR;310719; 261,83+;"
"300040082800016356293;07;EUR;010819; 261,83+;"
"300040082800016356390;01;EUR;310719; 183,16+;"
"300040082800016356390;07;EUR;010819; 183,16+;"
"130880909300350900060;01;EUR;300719; 20127893,81+;"
130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; "29,64+;"
130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; "39,36+;"
130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; "58,94+;"
130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; "68,88+;"
130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; "296,34+;"
130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; "321,21+;"
as you can see, some " are missing on the first line

I cam up with a program to add " to every line :

Code: Select all

setLocal EnableDelayedExpansion

for /f "delims=" %%a in ('type *.txt') do (
	set test="%%a
	echo !test! >> fichier_texte_traitee.txt
)
So it gives :
""300040082800016351540;01;EUR;310719; 84,67-;"
""300040082800016351540;07;EUR;010819; 84,67-;"
""300040082800016356293;01;EUR;310719; 261,83+;"
""300040082800016356293;07;EUR;010819; 261,83+;"
""300040082800016356390;01;EUR;310719; 183,16+;"
""300040082800016356390;07;EUR;010819; 183,16+;"
""130880909300350900060;01;EUR;300719; 20127893,81+;"
"130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; "29,64+;"
"130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; "39,36+;"
"130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; "58,94+;"
"130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; "68,88+;"
"130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; "296,34+;"
"130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; "321,21+;"
So now, i want to replace the "" with ", but i've been searching and trying for hours, i can't find a way to do this...

Any help would be very appreciated :D

Thank you in advance.

Re: Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 06:15
by miskox
This seems to work:

Code: Select all

@echo off
set var=""test"

set new_var=%var:""="%

echo _%var%_
echo _%new_var%_
Result:

Code: Select all

_""test"_
_"test"_
Saso

Re: Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 07:22
by afastboi
Hello !

Thanks for your answer, but it doesn't work for me

I tried to implement it like this :

Code: Select all

setLocal EnableDelayedExpansion

for /f "delims=" %%a in ('type *.txt') do (
	set var=%%a
	set iable=%var:""="%
	
	echo %iable%
)
when echoing "iable", it looks like it is empty...

Re: Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 10:16
by aGerman
You're tryin to update and expand variables inside of the body of the loop where variables are expanded only once before the loop is executed. If you use delayed variable expansion to work around this issue, then enclose variables into exclamation points rather than into percent signs.

Code: Select all

for /f "delims=" %%a in ('type *.txt') do (
	set var=%%a
	echo !var:""="!
)
Steffen

Re: Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 13:45
by Squashman

Code: Select all

@echo off

(for /f "delims=" %%G in (input.txt) do (
	echo %%G|findstr /B ^""" ||echo "%%G
))>Output.txt

Re: Replacing "" by " in every line of a text file

Posted: 23 Oct 2020 15:39
by dbenham
I am pretty sure your stated goal is not what you really want. Even if you manage to convert paired double quotes into a single double quote, the last lines will still have unbalanced quotes because the last field was already quoted.

It looks to me like your source file is a corrupted CSV using semicolon as a delimiter. Since none of your values contain any semicolons, you should be able to simply remove all quotes

Code: Select all

@echo off
setlocal enableDelayedExpansion
>output.csv (
  for /f "delims=" %%L in ('type *.txt 2^>nul') do (
    set "ln=%%L"
    echo(!ln:"=!
  )
)
--OUTPUT--

Code: Select all

300040082800016351540;01;EUR;310719; 84,67-;
300040082800016351540;07;EUR;010819; 84,67-;
300040082800016356293;01;EUR;310719; 261,83+;
300040082800016356293;07;EUR;010819; 261,83+;
300040082800016356390;01;EUR;310719; 183,16+;
300040082800016356390;07;EUR;010819; 183,16+;
130880909300350900060;01;EUR;300719; 20127893,81+;
130880909300350900060;04;EUR;010819;310719;0000360 ;0000000;02; 29,64+;
130880909300350900060;04;EUR;010819;310719;0000356 ;0000000;02; 39,36+;
130880909300350900060;04;EUR;310719;310719;0000358 ;0000000;45; 58,94+;
130880909300350900060;04;EUR;010819;310719;0000362 ;0000000;02; 68,88+;
130880909300350900060;04;EUR;010819;310719;0000366 ;0000000;02; 296,34+;
130880909300350900060;04;EUR;010819;310719;0000368 ;0000000;02; 321,21+;
Better yet, fix the code that is generating the corrupted data in the first place.

I suspect the trailing ; in my output is also not correct, but it could be. Also the leading or trailing space in some fields might be a problem, depending on what software is using the resultant CSV.

If I am wrong about CSV, then tell us what file format you are trying to create, and show us exactly how each line should look. Think hard about how the resultant file will be used.


Dave Benham

Re: Replacing "" by " in every line of a text file

Posted: 26 Oct 2020 06:37
by afastboi
Wow, thank you for the answers everyone !

@dbenham i didn't think of that at all.
I am in apprenticeship, and is the only "tech guy" on my field.
The first output i gave with the " placed in a wrong position are given by a robot, but i can not access it / modify it.

The file is then used by a program to input the lines in a excel file. I will try using the lines without any " with the program, and keep you informed.

Thanks again :) !