Solved conditional concatenation of records in a file

February 7, 2017 at 11:16:51
Specs: Windows 7
Hi All,

I am trying to write a batch script which will conditionally concatenate the records in the file, I was able to find few batch scripts online but the batch programs are concatenation all the records.

Can any one suggest me some logic on how to achieve the below.

I have a directory with a list of .txt files, each file will have data as below. I am trying to join the record with starts with 3 with the previous record which starts with 2.

Sample data pasted below.

Source files data

1|123|
2|Source|T|2016-01-01
3|CDR|XYZ|||||||||||||
3|CDR|XYZ|||||||||||||
4|||||||


2|Source|T1|2016-02-01
3|CDR1|XYZ1|||||||||||||
3|CDR2|XYZ2|||||||||||||
3|CDR1|abc1|||||||||||||
3|CDR2|abc2|||||||||||||
4|||||||

2|Source|T2|2016-03-01
3|CDR3|XYZ3|||||||||||||
3|CDR4|XYZ4|||||||||||||
4|||||||
5||||||||

Expected Output

1|123|

3|CDR|XYZ|||||||||||||2|Source|T|2016-01-01
3|CDR|XYZ|||||||||||||2|Source|T|2016-01-01
4|||||||

3|CDR1|XYZ1|||||||||||||2|Source|T1|2016-02-01
3|CDR2|XYZ2|||||||||||||2|Source|T1|2016-02-01
3|CDR1|abc1|||||||||||||2|Source|T1|2016-02-01
3|CDR2|abc2|||||||||||||2|Source|T1|2016-02-01
4|||||||


3|CDR3|XYZ3|||||||||||||2|Source|T2|2016-03-01
3|CDR4|XYZ4|||||||||||||2|Source|T2|2016-03-01
4|||||||
5||||||||


Thanks & Regards,
Sandhya


See More: conditional concatenation of records in a file

Report •

#1
February 8, 2017 at 05:19:12
working on it:

for /f %a in ('dir /b ^| findstr ".txt"') do (for /f %A in ('type %a ^| findstr "Source"') do (echo %A))
2|Source|T|2016-01-01
2|Source|T1|2016-02-01
2|Source|T2|2016-03-01

it'll take me a while

Im currently working on putting everything in varaibles, then reorganising the strings:

::start of test1.bat

@echo off
setlocal EnableDelayedExpansion
set cnt=0
for /f %%a in ('dir /b ^| findstr ".txt"') do (for /f %%A in ('type %%a ^| findstr "Source"') do (set /a cnt+=1&set var!cnt!=%%A))
echo !var1! !var2! !var3!&pause>nul

::end of test1.bat

sample in/output from cmd:

test1
2|Source|T|2016-01-01 2|Source|T1|2016-02-01 2|Source|T2|2016-03-01

Simple solutions are often the best

message edited by hidde663


Report •

#2
February 8, 2017 at 06:02:24
update:

::start of test1.bat
@echo off&setlocal EnableDelayedExpansion
for /f %%a in ('dir /b ^| findstr ".txt"') do (
for /f "tokens=1-4 delims=^|" %%A in ('type %%a') do (
if '"%%A"'=='"1"' (echo %%A^|%%B>>output.txt&echo.>>output.txt)
if '"%%A"'=='"2"' (set "Source=%%A|%%B|%%C|%%D")
if '"%%A"'=='"3"' (echo %%A^|%%B^|%%C^|^|^|^|^|^|^|^|^|^|^|^|^|!Source!>>output.txt)
if '"%%A"'=='"4"' (echo %%A^|^|^|^|^|^|^|>>output.txt&echo.>>output.txt)
if '"%%A"'=='"5"' (echo %%A^|^|^|^|^|^|^|^|>>output.txt)
)
)
::end of test1.bat

when run from folder (with 1 .txt file):

::start of output.txt
1|123

3|CDR|XYZ|||||||||||||2|Source|T|2016-01-01
3|CDR|XYZ|||||||||||||2|Source|T|2016-01-01
4|||||||

3|CDR1|XYZ1|||||||||||||2|Source|T1|2016-02-01
3|CDR2|XYZ2|||||||||||||2|Source|T1|2016-02-01
3|CDR1|abc1|||||||||||||2|Source|T1|2016-02-01
3|CDR2|abc2|||||||||||||2|Source|T1|2016-02-01
4|||||||

3|CDR3|XYZ3|||||||||||||2|Source|T2|2016-03-01
3|CDR4|XYZ4|||||||||||||2|Source|T2|2016-03-01
4|||||||

5||||||||
::end of output.txt

I will continue working on it tommorow, could you please give me some feedback on the current script?

does it work?

what needs to be changed?

Simple solutions are often the best

message edited by hidde663


Report •

#3
February 8, 2017 at 11:02:55
✔ Best Answer
This seemed to work on your sample:
::---- begin script
@echo off & setlocal enabledelayedexpansion
:: set name of working subdirectory
set work=temp
:: ----- create the temporary directory here. Dir. name not important.
md %work% 2>nul
for %%x in (*.txt) do call :mix "%%x"
goto :eof

:mix
for /f "tokens=1* delims=[]" %%a in ('find /v /n ""^<%1') do (
set x=%%b
set b=!x:~0,1!
set x=!x:~2!

if "!b!" equ "2" (
set hold=!x!
) else (
if "!b!" equ "3" (
>>%work%\%1 echo %%b^|!hold!
) else (
>>%work%\%1 echo.%%b
)
)
)
::---- end.
Although I normally hate nesting 'IF/ELSE' like that, it seemed easiest this time.
You will note that the output all goes to a temporary holding directory 'TEMP'.
Change that name if it conflicts with any existing structure. run the script in the
same directory as the .txt files of course.

message edited by nbrane


Report •

Related Solutions

#4
February 8, 2017 at 11:36:39
Thanks a lot for both of you for helping me. I was trying on some code to make it work but it was not working as expected.

I have tried your code and both are working fine.

Thanks for your time and help


Report •

Ask Question