Merging TXT records depending on data

November 10, 2009 at 17:23:43
Specs: Windows XP
Hi once more,

I have a TXT file that has groups of records that look like this:

Flight\6807
ICAO\(VHHH - CYVR)
Departure\8:00
Arrival\23:55
Equipment\B747-400 (C-GCBL)
Distance\12870.6mi
Days Flown\Su M T W Th F S
Route\OCEAN1.V2 ELATO A1 APU R583 BISIS G581 MJE A590 PIPPA
Route\G469 SPY R591 CHUUK R341 ODK B327 TRK18 YZP J523 YZT.KEINN4
Notes\Class 5 Flight

I need to merge the ROUTE\ records under each FLIGHT, so each flight has one route record. A space is used as delimiter between the waypoints.

As always, help is greatly appreciated, VBScript or BATCH files preferred.


See More: Merging TXT records depending on data

Report •


#1
November 10, 2009 at 21:08:50
(not tested)

setlocal enabledelayedexpansion
set new=
for /f "tokens=1 delims=" %%a in (flights) do (
set line=%%a
set rt=!line:~0,6!
if /i !rt! equ "route/" (
:note: there is a SPACE after the last exclm in foll line!
set new=!new!!line:~6!
) else (
if "!new!" neq "" set line="Route/"!new!
echo !line! >> outfil
set new=)
)


Report •

#2
November 11, 2009 at 09:43:09
Sorry, it didn't work. The code added a SPACE at the end of ROUTE records.

Here is a file with a few test cases:

Flight\6608
Route\W1 KRT RAMEI A202 SIKOU.SIKOU1
Notes\Class 4 Flight
Flight\4818
Route\BUTOP W30W ASARI A466 AMDAR A114 TRZ A466 TMD A66 ODIVA
Route\B824 URL G3 FV R11 TU R58 NOTAR UP607 TEB UN623 FLS UG3 VALDI
Route\G3 KEF 5E20 OZN 5E23 5950N 5E22 HO N312A MT T597 YXI J597 YSO
Route\SIMCO2
Notes\Class 5 Flight
Flight\6807
Route\OCEAN1.V2 ELATO A1 APU R583 BISIS G581 MJE A590 PIPPA
Route\G469 SPY R591 CHUUK R341 ODK B327 TRK18 YZP J523 YZT.KEINN4
Notes\Class 5 Flight
Flight\1009
Route\DIRECT
Notes\Class 1 Flight-Leg 10 of the Pan American Charter.


Desired output:

Flight\6608
Route\W1 KRT RAMEI A202 SIKOU.SIKOU1
Notes\Class 4 Flight
Flight\4818
Route\BUTOP W30W ASARI A466 AMDAR A114 TRZ A466 TMD A66 ODIVA B824 URL G3 FV R11 TU R58 NOTAR UP607 TEB UN623 FLS UG3 VALDI G3 KEF 5E20 OZN 5E23 5950N 5E22 HO N312A MT T597 YXI J597 YSO SIMCO2
Notes\Class 5 Flight
Flight\6807
Route\OCEAN1.V2 ELATO A1 APU R583 BISIS G581 MJE A590 PIPPA G469 SPY R591 CHUUK R341 ODK B327 TRK18 YZP J523 YZT.KEINN4
Notes\Class 5 Flight
Flight\1009
Route\DIRECT
Notes\Class 1 Flight-Leg 10 of the Pan American Charter.


I just noticed that there is already a SPACE at the end of ROUTE records that have a continuation, so there is no need to worry about adding a delimiter.


Report •

#3
November 11, 2009 at 10:27:43
nbrane, I am using a backslask, not a slash. so I editted the following lines:

if /i !rt! equ "Route\" (
if "!new!" neq "" set line="Route\"!new!


Report •

Related Solutions

#4
November 11, 2009 at 11:30:36
Using your code as starting point:
- I added indentations,
- removed the SPACE that was appended to all records,
- changed the WRITE section a bit.

@ECHO OFF
setlocal enabledelayedexpansion
set new=""
for /f "tokens=1 delims=" %%a in (CVAstep7.txt) do (
  set line=%%a
  set rt=!line:~0,6!
  echo "<!rt!>"
  if /I !rt! equ "Route\" (
    echo Process route record
    set new=!new!!line:~6!
  ) else (
    if !new! neq "" (
      echo Write ROUTE record
      set route=Route\!new!
      echo !route!>>CVAstep8.txt
      set new="")
    echo Write current record
    echo !line!>>CVAstep8.txt
  )
)

The IF does not select any Route\ records for some reason. I displayed the contents of RT and it is Route\.


Report •

#5
November 11, 2009 at 12:51:35
apologies, a couple of MY mistakes I saw (i'm new at this, thank you in advance for your patience).

set route="Route\"!new!
should not have had quotes around Route\:
set route=Route\!new!

EDIT: I displayed the record type stored in RT and it is Route\,so the error resides in the IF statement.

exactly, the real messup (mine, not yours) is here:
if /i !rt! equ "Route\" (
this one SHOULD have quotes, around the var:
if /i "!rt!" equ "Route\" (

i'm about sure this one is what's holding everything up.
the first error will just put quotes around the Route\ on output but not hold things up.
I will run a test on my end to make sure everything works as it should, but these two fixes will hopefully unblock the pipeline.

"- changed the WRITE section a bit."

you also picked up on a really dumb mistake i made in the logic where i overwrote the !line! before output. good detective work! and apologies for not getting it right.


Report •

#6
November 11, 2009 at 13:12:59
No apology needed, you were clear it was untested.

I'm a mainframe programmer by trade and have done PC programming since BASIC on the Apple II, I just need a push in the right direction. :)

The IF was holding me up, much further now. It's not perfect but I have some testing to do.


Report •

#7
November 11, 2009 at 13:33:10
WOOHOO!!!

Thank you very much, I just had to clean up minor errors I put in myself and everything works perfectly now.


Report •

#8
November 11, 2009 at 14:12:31
glad its working now!

"I'm a mainframe programmer by trade and have done PC programming since BASIC on the Apple II, I just need a push in the right direction. :)"

i guess that makes us both "old school". I actually wrote Pascal on punch-cards in my first round of college (1977-8)
and suffered COBOL in my two-year college on hp series iii
(op sys MPE or something like that) in 1985-6.


Report •

#9
November 13, 2009 at 20:43:10
if you can download gawk for windows (see my sig), here's a one liner
C:\test>gawk "/^Flight\\|Notes/{printf \"\n%s\n\",$0;next }{ printf \"%s \",$0}" file

Flight\6608
Route\W1 KRT RAMEI A202 SIKOU.SIKOU1
Notes\Class 4 Flight

Flight\4818
Route\BUTOP W30W ASARI A466 AMDAR A114 TRZ A466 TMD A66 ODIVA Route\B824 URL G3 FV R11 TU R58 NOTAR UP607 TEB UN623 FLS
UG3 VALDI Route\G3 KEF 5E20 OZN 5E23 5950N 5E22 HO N312A MT T597 YXI J597 YSO Route\SIMCO2
Notes\Class 5 Flight

Flight\6807
Route\OCEAN1.V2 ELATO A1 APU R583 BISIS G581 MJE A590 PIPPA Route\G469 SPY R591 CHUUK R341 ODK B327 TRK18 YZP J523 YZT.K
EINN4
Notes\Class 5 Flight

Flight\1009
Route\DIRECT
Notes\Class 1 Flight-Leg 10 of the Pan American Charter.

GNU win32 packages | Gawk


Report •

#10
November 14, 2009 at 07:10:41
if you can download gawk for windows (see my sig), here's a one liner

C:\test>gawk "/^Flight\\|Notes/{printf \"\n%s\n\",$0;next }{ printf \"%s \",$0}" file

Flight\4818
Route\BUTOP W30W ASARI A466 AMDAR A114 TRZ A466 TMD A66 ODIVA Route\B824 URL G3 FV R11 TU R58 NOTAR UP607 TEB UN623 FLS
UG3 VALDI Route\G3 KEF 5E20 OZN 5E23 5950N 5E22 HO N312A MT T597 YXI J597 YSO Route\SIMCO2
Notes\Class 5 Flight

Interesting, but Route\ gets replicated on the single line as well.

I"ll keep it in mind for future use though, it would make some tasks easier.


Report •

#11
November 14, 2009 at 08:23:54
that's easy to fix. anyway, not going to do it.

GNU win32 packages | Gawk


Report •


Ask Question