Computing.Net > Forums > Programming > Batch change a char after nth pos

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch change a char after nth pos

Reply to Message Icon

Name: Robert Hedan
Date: November 9, 2009 at 20:50:20 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Robert Hedan
Date: November 9, 2009 at 21:05:33 Pacific
Reply:

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


0

Response Number 2
Name: Judago
Date: November 9, 2009 at 22:44:07 Pacific
Reply:

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


1

Response Number 3
Name: Robert Hedan
Date: November 10, 2009 at 01:32:52 Pacific
Reply:

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.


0

Response Number 4
Name: Robert Hedan
Date: November 10, 2009 at 01:54:32 Pacific
Reply:

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)


0

Response Number 5
Name: Judago
Date: November 10, 2009 at 04:19:41 Pacific
Reply:

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


1

Related Posts

See More



Response Number 6
Name: Robert Hedan
Date: November 10, 2009 at 12:16:33 Pacific
Reply:

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


0

Response Number 7
Name: Judago
Date: November 10, 2009 at 13:48:13 Pacific
Reply:

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


1

Response Number 8
Name: Robert Hedan
Date: November 10, 2009 at 13:57:46 Pacific
Reply:

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.


0

Response Number 9
Name: Judago
Date: November 10, 2009 at 21:16:46 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch change a char after nth pos

Batch ,read a word after : in txt www.computing.net/answers/programming/batch-read-a-word-after-in-txt/16896.html

Converting a String to a char array www.computing.net/answers/programming/converting-a-string-to-a-char-array/3190.html

Converting a char** to a char* www.computing.net/answers/programming/converting-a-char-to-a-char/6637.html