Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to remove first 18 chars from txt file

I have a .txt document with over 32,000 lines of commented machine code. It looks like this:

Display menu window
C0/000E:    E220        SEP #$20
C0/0010:    C210        REP #$10
C0/0012:    20640B      JSR $0B64
C0/0015:    20750B      JSR $0B75
C0/0018:    C220        REP #$20
C0/001A:    A90001      LDA #$0100

I need to convert the code as follows for compiling purposes:

; Display menu window
SEP #$20
REP #$10
JSR $0B64
JSR $0B75
REP #$20
LDA #$0100

Specifically, that means:

  • Blank lines must remain unchanged.
  • If a line starts with "C0/" then the first 18 characters are to be deleted, including tabs.
  • Otherwise, it's a function title, so add a semi-colon followed by a space at the beginning (not mandatory).

Any help would be greatly appreciated.

like image 341
Sheldon M. Avatar asked Feb 10 '23 15:02

Sheldon M.


1 Answers

The Batch file below is a different approach that may run faster than other similar methods, but this largely depends on the size of the file:

@echo off

for /F "tokens=1-2*" %%a in ('findstr /N "^" test.txt') do (
   for /F "tokens=1,2 delims=:/" %%d in ("%%a") do (
      if "%%e" equ "C3" (
         echo %%c
      ) else if "%%e" neq "" (
         echo ; %%e %%b %%c
      ) else (
         echo/
      )
   )
)

However, the fastest method is via a Batch-JScript hybrid script. Save the file below with .bat extension:

@set @Batch=1    /*
@cscript //nologo //E:JScript "%~F0" < test.txt
@goto :EOF & rem */

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace
   (/^C3\/.{15}|^(..)/gm,function(A){return A.length==2?"; "+A:""}));
like image 55
Aacini Avatar answered Feb 12 '23 07:02

Aacini