Conditional Concatenation Of Records In A File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ss0703259
Posts: 1
Joined: 07 Feb 2017 16:27

Conditional Concatenation Of Records In A File

#1 Post by ss0703259 » 07 Feb 2017 16:33

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

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

Re: Conditional Concatenation Of Records In A File

#2 Post by b0gus » 15 Feb 2017 06:49

ss0703259 wrote:Can any one suggest me some logic on how to achieve the below.
try this:

Code: Select all

@echo off & cls & SetLocal EnableExtensions EnableDelayedExpansion

:enter_dir
  set /p "dir_source=[c:\dir1\dir2...] enter dir with source file's="
  if NOT defined dir_source goto enter_dir

if "%dir_source:~-1%" == "\" set "dir_source=%dir_source:~0,-1%"
if NOT exist "%dir_source%\*.txt" (
  echo:*.txt file's not found in '%dir_source%\'
  pause & exit /b 1
)
if NOT exist "%dir_source%\results" md "%dir_source%\results"||(
  echo:can't make dir '%dir_source%\results'
  pause & exit /b 2
)

for /f "tokens=*" %%v in ('dir /b ".\%dir_source%\*.txt"') do (set "line_source="
    <"%dir_source%\%%v" >"%dir_source%\results\%%v" (for /f "tokens=*" %%L in ('find /n /v ""') do (set "line=%%L" & set "line=!line:*]=!"
      if "!line!" == "" (
        echo:!line!
      ) else (
        if "!line:~0,2!" == "2|" (
          set "line_source=!line!"
        ) else (
          if "!line:~0,2!" == "3|" set "line=!line!!line_source!"
          echo:!line!
        )
      )
    )
  )
)
echo:see results in dir '%dir_source%\results'
pause
exit /b 0

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

Re: Conditional Concatenation Of Records In A File

#3 Post by Compo » 15 Feb 2017 15:32

ss0703259 wrote: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||||||||

Are you sure? my best guess would be the following:

Code: Select all

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

Post Reply