Reputation: 21
I have two text files as A.txt and B.txt with the below contents:
A.txt
value_a1,value_a2
value_b
value_c
value_d
value_e1,value_e2
B.txt
12,14
13
15
16
23,34
I want output file C.txt as
"value_a1","12","value_a2","14"
"value_b","13"
"value_c","15"
"value_d,"16"
"value_e1,"23","value_e2","34"
Please guide me through as I am new to Batch Script.
Upvotes: 1
Views: 3737
Reputation: 130919
There are many restrictions to this solution, but it is about as simple a solution as is possible with pure native batch. It is also fairly efficient for a batch solution.
@echo off
setlocal enableDelayedExpansion
<b.txt >c.txt (
for /f delims^=^ eol^= %%L in (a.txt) do (
set "ln="
set /p "ln="
set "out="
for %%A in (%%L) do (
for /f "tokens=1* delims=," %%a in ("!ln!") do (
set "out=!out!,"%%A","%%a""
set "ln=%%b"
)
echo !out:~1!
)
)
)
Limitations:
*
or ?
or !
<space>
<tab>
,
;
=
!
,
(,
is strictly a delimiter)Some of the limitations can be overcome fairly easily. Others require a lot more effort, to the point of becoming totally impractical.
Upvotes: 2
Reputation: 484
Following code will work:
@Echo off
echo. >>d.txt
type b.txt >>d.txt
set dec=1
For /F "usebackq tokens=1,* delims=, " %%a in ("a.txt") do call :File1 %%a %%b
set dec1=0
del d.txt
exit /b
:File1
SET str1=%~1
SET str2=%~2
SET Count=1
For /F "usebackq tokens=1,* skip=%dec% delims=," %%A in ("d.txt") do call :File2 %%A %%B
set /a dec=%dec%+1
exit /b
:File2
SET str3=%~1
SET str4="%~2"
IF %Count% EQU 1 (
IF %str4%=="" (
echo "%str1%","%str3%" >>c.txt
set /a Count=%Count%+1
) ELSE (
echo "%str1%","%str3%","%str2%",%str4% >>c.txt
set /a Count=%Count%+1)
)
exit /b
Upvotes: 3