Jump to content

How to merge two text files?


Recommended Posts


Never mind :P I'll let you know if I encounter any problems.

By the way, is

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

any different from

FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b

?

Link to comment
Share on other sites

Never mind :P I'll let you know if I encounter any problems.

By the way, is

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

any different from

FOR /F "tokens=1,2 delims=, " %%a in ('FINDSTR "^" 1.inf') do echo %%a,%%b

?

Yes it is, one uses TYPE, the other uses FINDSTR :whistle:;)

More seriously, try them on a UNICODE .inf and check the differences.

See here:

http://www.robvanderwoude.com/type.php

Convert Unicode to ASCII vv

TYPE is UNICODE compliant (and the result will normally be "plain text"), FINDSTR is not.

jaclaz

Link to comment
Share on other sites

Try with a "plain":

FOR /F "tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

I was wrong. It still doesn't ECHO lines starting with ";" when EOL isn't defined :(

I'll probably use:

  1. FOR /F delims^=^ eol^= %%A IN (1.inf) DO ECHO %%A


    when I want to display full lines and

  2. FOR /F tokens^=1-2^ eol^=^

    delims^=^,^ %%A IN (1.inf) DO ECHO %%A,%%B


    when I need to specify delims, one of which is space.

Edited by tomasz86
Link to comment
Share on other sites

I was wrong. It still doesn't ECHO lines starting with ";" when EOL isn't defined :(

Yes, the semicolon is the default EOL of the FOR, you can do something like:

FOR /F "eol=§ tokens=1,2 delims=, " %%a in ('TYPE 1.inf') do echo %%a,%%b

The § character is in "Windows", in command line it will "look" different, it's ALT+0167, but you can use any other "extended ASCII" character that won't be normally used in a .inf.

jaclaz

Link to comment
Share on other sites

You seem to like "§" a lot :D but do you remember that it was also present in your original SPLITINF script... and the problem was that it didn't work when system locale was set to Korean :( and in the end I had to replace it with something else which was "}#}" so I'd like to avoid using such characters.

Link to comment
Share on other sites

You seem to like "§" a lot :D but do you remember that it was also present in your original SPLITINF script... and the problem was that it didn't work when system locale was set to Korean :( and in the end I had to replace it with something else which was "}#}" so I'd like to avoid using such characters.

Naah it's only because it is easily available on my italian keyboard, as said it is up to you to find a character that is not used in "Korean" (or "other rare language" files).

BTW it is possible (but you will have to experiment) that you can use one of the "lower" ASCII Characters, in the range 0 to 31 decimal, really cannot say. :unsure:

jaclaz

Link to comment
Share on other sites

  • 2 weeks later...

I wanted to use both double quotes and spaces as delimiters but I didn't know how to do it so I searched for a solution in the Internet but couldn't really find anything useful. Finally I've managed to figure it out by myself and I'm posting this in case someone else encounters a similar problem:

FOR /F delims^=^"^  %%A IN ("abc = "1"") DO ECHO "%%A"

It will display just

"abc"

Notice that there are two spaces before %%A.

Link to comment
Share on other sites

The example you have provided shows us only that the space has been delimited because it has split the string before it has seen your double quote and output only the first token.

See how these work:


@ECHO OFF & SETLOCAL ENABLEEXTENSIONS
ECHO/DELIMITED DOUBLE QUOTES ONLY
FOR /F TOKENS^=1-3^ DELIMS^=^" %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C]
FOR /F USEBACKQ^ TOKENS^=1-3^ DELIMS^=^" %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C]
ECHO/&ECHO/DELIMITED DOUBLE QUOTES AND SPACES
FOR /F TOKENS^=1-6^ DELIMS^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]
FOR /F USEBACKQ^ TOKENS^=1-6^ DELIMS^=^"^ %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]
PAUSE & GOTO :EOF

Link to comment
Share on other sites

Actually, if all tomasz86 is interested in is the first token in the string, then his code does seem to work, as shown by running this (NOTE two spaces before "%%A"):

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS
ECHO/TOMASZ86 METHOD FOR FIRST TOKEN ONLY
FOR /F delims^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A]
FOR /F delims^=^"^ %%A IN ("THIS"TEST" IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A]
ECHO/&ECHO/DELIMITED DOUBLE QUOTES ONLY
FOR /F TOKENS^=1-3^ DELIMS^=^" %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C]
FOR /F USEBACKQ^ TOKENS^=1-3^ DELIMS^=^" %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C]
ECHO/&ECHO/DELIMITED DOUBLE QUOTES AND SPACES
FOR /F TOKENS^=1-6^ DELIMS^=^"^ %%A IN ("THIS IS"ENCLOSED"IN DOUBLE QUOTES") DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]
FOR /F USEBACKQ^ TOKENS^=1-6^ DELIMS^=^"^ %%A IN ('THIS IS"ENCLOSED"IN DOUBLE QUOTES') DO ECHO/[%%A] [%%B] [%%C] [%%D] [%%E] [%%F]
PAUSE & GOTO :EOF

The output for both lines of the code added to show his method is -- [THIS]

If you want more flexibility over getting more than just the first token, then your code is much more complete and accurate, Yzöwl, as always.

EDIT: If, however, he was trying to get the same result for VAR=VAL vs VAR = VAL vs VAR="VAL" vs VAR = "VAL", then this code would work:

@ECHO OFF & SETLOCAL ENABLEEXTENSIONS
ECHO GET SAME RESULT FOR VAR=VAL vs VAR = VAL vs VAR="VAL" vs VAR = "VAL"
FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc=1") DO ECHO/[%%A] [%%B]
FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc = 1") DO ECHO/[%%A] [%%B]
FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc="1"") DO ECHO/[%%A] [%%B]
FOR /F TOKENS^=1-2^ DELIMS^=^=^"^ %%A IN ("abc = "1"") DO ECHO/[%%A] [%%B]
PAUSE & GOTO :EOF

All four lines will echo

[abc] [1]

So it all depends on what he really wants to do.

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

Hmm, actually the strings go like this:

1.txt


abc = 1
abc= 1
abc =1
abc = "1 2"
abc= "1 2"
abc ="1 2"
etc.

They're not consistent so sometimes there are quotes in other cases there aren't. Some goes for spaces.

I'm using this script:

FOR /F tokens^=1*^ delims^=^"^=^  %%A IN ('TYPE "1.txt"') DO (
FOR /F delims^=^" %%C IN ("%%B") DO ECHO %%A="%%C"
)

and the output is:


abc="1"
abc="1"
abc="1"
abc="1 2"
abc="1 2"
abc="1 2"

Link to comment
Share on other sites

Well, the output would be the same with this:

FOR /F "tokens=1,2 delims==" %%A IN ('TYPE "1.txt"') DO CALL :strip_readd_quotes %%A %%B
GOTO :EOF

:strip_readd_quotes
ECHO %1="%~2"
GOTO :EOF

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Thanks :thumbup

Yours is actually better because it works even for something like this:

MainCancelIntroString   = "Thank you for reporting the Request. When you click ""Send Report"" button, data concerning why install failed will be sent to Microsoft"

while mine doesn't.

Link to comment
Share on other sites

Thanks :thumbup

Yours is actually better because it works even for something like this:

Yes and no.

If you have more than two tokens NOT quoted "on the right side" of the equal sign my code snippet won't work.

This may:

FOR /F "tokens=1,* delims==" %%A IN ('TYPE "1.txt"') DO (
CALL :strip_spaces %%A
CALL :strip_readd_quotes %%B
ECHO !var!=!val!
)


:strip_spaces
set var=%*
GOTO :EOF

:strip_readd_quotes
set val="%*"
set val=%val:""="%
IF "[]"=="[%*]" set val=
GOTO :EOF

but it won't work with the example you just posted with double-double quotes inside double quotes. :ph34r:

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...