Batch change a char after nth pos

November 9, 2009 at 20:50:20
Specs: Windows XP
I need to split "FLIGHT" records in a TXT file starting after 11th position, and identify next record as "ICAO".

Sample input
Flight§6805(ZBAA - RJAA)
Departure§17:45
Flight§6812(YSSY - PHNL)
Departure§06:30

Desired output:
Flight§6805
ICAO§(ZBAA - RJAA)
Departure§17:45
Flight§6812
ICAO§(YSSY - PHNL)
Departure§06:30

I know how to execute BAT files, but do not know detailed syntax. This is to help a virtual airline manage a database.

Thank you for your time.


See More: Batch change a char after nth pos

Report •


#1
November 9, 2009 at 21:05:33
I already have this from another thread to split a record at "#":

@Echo OFF

For /f "tokens=1,* delims=#" %%a in (%*) Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

:SPLIT
For /f "tokens=1,* delims=#" %%a in ("%*") Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

I just don't know how to incorporate the split after the 11th character for records starting in "FLIGHT".


Report •

#2
November 9, 2009 at 22:44:07
Maybe this will do it:

@ECHO OFF
for /f "usebackq delims=" %%a in ("your text file") do (
    echo %%a|find /i "flight"
    if errorlevel 1 (
        >>"newfile" echo %%a
    ) else (
        for /f "tokens=1,2 delims=^(" %%b in ("%%a") do (
            >>"newfile" echo %%b
            >>"newfile" echo ICAO§^(%%c
        )
    )
)


Batch Variable how to


Report •

#3
November 10, 2009 at 01:32:52
Close, it splits the FLIGHT record and adds in the new ICAO record as requested, but it also adds a phantom ICAO record further down.

I'm going to try something.


Report •

Related Solutions

#4
November 10, 2009 at 01:54:32
I wanted to keep the request as simple as possible, but it seems I have to show you all the input records 'cause it's interfering with your code, sorry.

This is a first series of input records, they'll eventually become one database record, somehow. :D

Flight§6805(ZBAA - RJAA)
Departure§17:45
Arrival§21:00
Equipment§MD-11 (C-FCAL)
Distance§1329.43mi
Days Flown§Su M T W Th F S
Route§VYK A326 DONVO G597 SEL G585 JEC G597 VENUS.VENUS
Notes§Class 5 Flight
Line1§Delete
Line2§Delete
Line3§Delete

The code split the FLIGHT records, prefixed the following record with ICAO, but there's a phantom ICAO record after NOTES.

Flight§6805
ICAO(ZBAA - RJAA)
Departure§17:45
Arrival§21:00
Equipment§MD-11 (C-FCAL)
Distance§1329.43mi
Days Flown§Su M T W Th F S
Route§VYK A326 DONVO G597 SEL G585 JEC G597 VENUS.VENUS
Notes§Class 5 Flight
ICAO(
Line1§Delete
Line2§Delete
Line3§Delete

Also, the new ICAO record doesn't have the special character as identified in the code for some reason.

Desired:
ICAO§(ZBAA - RJAA)

Actual output:
ICAO(ZBAA - RJAA)


Report •

#5
November 10, 2009 at 04:19:41
I have fixed problem with records that contain "flight" elsewhere
in the line by using "findstr /b", but I can't replicate the missing delimiter....

The problem seems to be encoding related, my test output has
them. Batch generally only deals with printable ASCII characters,
some extended ansi and non printable characters will work sometimes....

Try pasting the delimiter into the script in place of the one I put in
and try that instead...

I have also tried changing the code page to what I'm using
because it seems to be working for me.

If it fails then there are other options to try.

@ECHO OFF
setlocal
for /f "tokens=2 delims=:" %%z in ('2^>nul chcp') do set cp=%%z
2>&1 >nul chcp 437
for /f "usebackq delims=" %%a in ("your text file") do (
    echo %%a|>nul findstr /i /b /l "flight"
    if errorlevel 1 (
        >>"newfile" echo %%a
    ) else (
        for /f "tokens=1,2 delims=^(" %%b in ("%%a") do (
            >>"newfile" echo %%b
            >>"newfile" echo ICAO§^(%%c
        )
    )
)
2>&1 >nul chcp%cp%


Batch Variable how to


Report •

#6
November 10, 2009 at 12:16:33
Thanks Judago, I'll try this last code.

About the delimiter, I switched to \ and things seem to work. I put them in myself during the extraction so I can use whatever suits your needs.

TEST RESULTS: Only the first FLIGHT record was not split, no more phantom ICAO record, everything else looks good at first glance.

If this is not possible to code, I can note the error and split record #1 manually.

Off-topic: Is there a freeware file comparison tool I can use to compare input and output text files?

We had utilities available for mainframe use, it would be nice to have something equivalent to check for changes, just to verify the changes I do at each step of file manipulation (there's many).


Report •

#7
November 10, 2009 at 13:48:13
About the delimiter, I can use any character that suits your needs. I put them in myself during the extraction. I tried using Æ, with same results, I suppose I'm limitted to something that falls within a certain range of ALT codes.

It's a long shot but you could try adding a ^ before the delimiter in the script.

If there are no tabs in your text I would suggest it as a delimiter instead.

Extended ansi character like those you are using normally tend to work, but what I would consider "safe" characters would be 09h and 20h - 7Eh(an exception being the " character, it *should* work in these circumstances but a for /f loop won't split on it).

TEST RESULTS: Only the first FLIGHT record was not split, no more phantom ICAO record, everything else looks good at first glance.

I can't seem to replicate this either, I think I'm going to try re-writing the code later(I have to go to work now) as these inconsistencies call for it. Unfortunately even then I see no way to avoid adding the delimiter to through the script.....

Off-topic: Is there a freeware file comparison tool I can use to compare input and output text files?

We had utilities available for mainframe use, it would be nice to have something equivalent to check for changes, just to verify the changes I do at each step of file manipulation (there's many).

If all you need to do check the file is *somehow* different using xp's "fc" command could work, failing that perhaps a win32 port of the unix/linux "diff" may do the trick.


Batch Variable how to


Report •

#8
November 10, 2009 at 13:57:46
Thanks for everything Judago.

No need to spend more time on this. This process will not be done often and this automation is already a HUGE bonus as it is.

I editted my post as you were responding, I now use "\" as a delimiter and it works fine.

During the recent tests I noticed an error had slipped into how I massaged the root extraction; FLIGHT was appended onto other record types by mistake. I will start a separate thread for that topic.

EDIT: I switched to OpenOfficeinstead of NotePad to do global changes and record 1 is now processed properly.


Report •

#9
November 10, 2009 at 21:16:46
Glad I could sort of help a little.. I think...

Yeah you do have to watch out for some characters in batch. I once had a script that couldn't find labels for goto's because some non-printable character made it's way in........

I guess OOo may have killed them off.


Batch Variable how to


Report •


Ask Question