Page 1 of 1
How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 21 Mar 2018 14:20
by goodywp
Hi all,
I have an long string in a text file as below:
"12345", "23456-1w3s", "abas-122", "9876-ads", "alfsajf;lda"
Now my task is to separate it into multiple lines based on the quotation in an another text file as below:
"12345"
"23456-1w3s"
"abas-122"
"9876-ads"
"alfsajf;lda"
I tired as be low code, but I found that there is no consistency and the output depend on the substring itself
Code: Select all
for /f "tokens=1-5 delims=," %%A in (fdata1.txt) do call :EachLine %%A %%B %%C %%D %%E
:EachLine
set a=%1
set b=%2
set c=%3
set d=%4
set e=%5
ECHO %d%>>echo_data_1.txt
ECHO %c%>>echo_data_1.txt
ECHO %a%>>echo_data_1.txt
ECHO %b%>>echo_data_1.txt
ECHO %e%>>echo_data_1.txt
some times, it works fine like as below:
"9876-ads"
"23456-1w3s"
"12345"
"abas-122"
"alfsajf;lda"
but sometimes it output as below:
ECHO is off.
ECHO is off.
"12345"
"abas-122"
ECHO is off.
looks like up to the substring itself, length...or complicated...?
Is any better way to process this kind of task as above?
Thanks
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 21 Mar 2018 14:50
by Squashman
I don't see why you would call to another function instead of just using echo to output the variables inside the FOR command.
You will need to provide better examples of when it works and when it fails. We have no insight otherwise.
You do need an exit or GOTO :EOF after your FOR and before your lablel. Otherwise that code will execute twice.
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 21 Mar 2018 15:14
by goodywp
Squashman wrote: ↑21 Mar 2018 14:50
I don't see why you would call to another function instead of just using echo to output the variables inside the FOR command.
You will need to provide better examples of when it works and when it fails. We have no insight otherwise.
You do need an exit or GOTO :EOF after your FOR and before your lablel. Otherwise that code will execute twice.
Let us say:
this is inside the fdata1.txt
Code: Select all
"Schemes are embedded according to the 'Schemes to Sign_for_Tetra_829552.doc' from scheme packs <T501-08680-0101>", "8295522003_E2EE.P3A.pkg, Prm file to be used: 8295522003_E2EE.P3A.jsat", "TELIUM3\E2EE_DLL", "8295522003_E2EE.P3A", "Copy signed file back to the original folder and remote download package zip file"
And this one is for fdata2.txt
Code: Select all
"Schemes are embedded according to the 'Schemes to Sign_for_Tetra_829500.doc' from scheme packs <T501-08680-0102,T501-08699-0100,0501-08690-0100>", "8295000512_KIA.P3A.pkg, Prm file to be used: 8295000512_KIA.P3A.jsat", "TELIUM3\KIA", "8295000512_KIA.P3A", "Copy signed file back to the original folder and remote download package zip file"
You can not tell the difference in terms of the format. I can have a expected output from fdata1.txt
But I can not get the expected one from fdata2.txt
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 21 Mar 2018 15:23
by dbenham
Well you will certainly have problems if any of your quoted strings contain commas.
As long as none of your values contain * or ?, I would use FOR to parse out each quoted string.
Code: Select all
>echo_data_1.txt (for /f "usebackq delims=" %%A in ("fdata1.txt") do for %%B in (%%A) do echo %%B)
Or for a more robust solution, you could use
JREPL.BAT to parse out each quoted string
Code: Select all
call jrepl "\q[\c\q]\q" "$txt=$0" /xseq /jmatchq /f "fdata1.txt" /o "echo_data_1.txt"
Dave Benham
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 22 Mar 2018 05:03
by b0gus
@dbenham, it seems to me that here the ONE closing parenthesis is superfluous:
Code: Select all
>echo_data_1.txt (for /f "usebackq delims=" %%A in ("fdata1.txt") do for %%B in (%%A) do echo %%B))
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 22 Mar 2018 05:12
by dbenham
Yep - that was a typo.
All fixed - thanks
Dave Benham
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 22 Mar 2018 05:38
by Compo
If you want to use the same structure, you could do it like this:
Code: Select all
@Echo Off
For /F "UseBackQ Delims=" %%A In ("fdata1.txt") Do Call :EachLine %%A
Exit /B
:EachLine
For %%A In (%*) Do (Set /P "=%%~A"<Nul&Echo=)>>"echo_data_1.txt"
GoTo :EOF
If you're happy to use a different structure then perhaps this would do it for you.
Code: Select all
@(For /F "UseBackQ Delims=" %%A In ("fdata1.txt") Do @For %%B In (%%A) Do @(
Set /P "=%%~B"<Nul&Echo=))>"echo_data_1.txt"
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 22 Mar 2018 08:15
by goodywp
Thank you so much for all the input....
I would like to use
Code: Select all
@Echo Off
For /F "UseBackQ Delims=" %%A In ("fdata1.txt") Do Call :EachLine %%A
Exit /B
:EachLine
For %%A In (%*) Do (Set /P "=%%~A"<Nul&Echo=)>>"echo_data_1.txt"
GoTo :EOF
The reason I choose this one is that after processing, the ```` has been automatically removed. My original code also has the re-ordering the lines as I want. Anyway, I use this sed command to do the job
Code: Select all
sed -n -e 4p -e 3p echo_data_2.txt > newdata_2.txt
Thanks
Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..
Posted: 22 Mar 2018 09:03
by Squashman
If you read the help file for the FOR or CALL commands all you have to do is use the ~ to remove the quotes. Doesn't matter what code you are using. Using CALL to a label will slow down your batch file. Faster to use the Nested FOR commands.