DosTips.com

A Forum all about DOS Batch
It is currently 26 May 2017 18:23

All times are UTC-06:00




Post new topic  Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 07 Feb 2017 16:33 
Offline

Joined: 07 Feb 2017 16:27
Posts: 1
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
Quote:
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
Quote:
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||||||||


Top
   
PostPosted: 15 Feb 2017 06:49 
Offline

Joined: 02 Mar 2016 12:58
Posts: 6
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


Top
   
PostPosted: 15 Feb 2017 15:32 
Offline

Joined: 21 Mar 2014 08:50
Posts: 401
ss0703259 wrote:
Expected Output
Quote:
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||||||||


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 3 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited