How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#1 Post by goodywp » 21 Mar 2018 14:20

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

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#2 Post by Squashman » 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.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#3 Post by goodywp » 21 Mar 2018 15:14

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#4 Post by dbenham » 21 Mar 2018 15:23

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

b0gus
Posts: 7
Joined: 02 Mar 2016 12:58

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#5 Post by b0gus » 22 Mar 2018 05:03

@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))

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#6 Post by dbenham » 22 Mar 2018 05:12

Yep - that was a typo.
All fixed - thanks

Dave Benham

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#7 Post by Compo » 22 Mar 2018 05:38

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"

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#8 Post by goodywp » 22 Mar 2018 08:15

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

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to separate long string with multiple quotations into multiple lines for each substring inside each quotation..

#9 Post by Squashman » 22 Mar 2018 09:03

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.

Post Reply