批处理脚本以编辑.txt文件 [英] Batch script to edit a .txt file

查看:143
本文介绍了批处理脚本以编辑.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.txt文档,其中包含超过32,000行带注释的机器代码.看起来像这样:

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       (Set 8-bit accumulator)
C0/0010:    C210        REP #$10       (Set 16-bit X and Y)

我有一个脚本,可将其转换如下,以进行编译:

I have a script that converts it as follows for compiling purposes:

; Display menu window
SEP #$20       (Set 8-bit accumulator)
REP #$10       (Set 16-bit X and Y)

问题是,我想将每个函数的首地址保留为标签.因此它应该看起来像这样:

The problem is, I'd like to keep the first address of every function as a label. So it should instead look like this:

; Display menu window
C0000E: SEP #$20       (Set 8-bit accumulator)
        REP #$10       (Set 16-bit X and Y)

具体来说,这意味着我需要一个脚本,该脚本将:

Specifically, that means I need a script that will:

  1. 只要一行的前3个字符不是C0/
  2. ,就停止
  3. 看下一行;如果以C0/开头,请继续,否则返回步骤1.然后跳过这一行.
  4. 在行的开头打印分号.有关此的更多信息.
  5. 将其设置为C0/XXXX:变为C0XXXX:
  6. 删除接下来的9个字符.
  7. 打印两个空格.
  1. Stop whenever the first 3 characters on a line are not C0/
  2. Look at the next line; if it starts with C0/, continue, otherwise return to step 1. and skip the line.
  3. Print a semi-colon at the start of the line. More info on this below.
  4. Make it so C0/XXXX: becomes C0XXXX:
  5. Delete the next 9 characters.
  6. Print two spaces.

其他脚本将由我负责.我需要在脚本中添加分号,因为我的实际脚本会在前面添加另一分号,因此我将能够使用查找和替换功能在所有位置安全地将它们都删除.

My other script will take care of the rest. I need the script to add a semi-colon because my actual script will add another semi-colon in front, so I'll be able to use find-and-replace to safely remove them both everywhere.

请注意,此开发板会自动将制表符转换为多个空格,因此,我提供的代码中的每一行与本地文件中的行长都不相同.

Note that this board automatically converts tabs into a number of spaces, so each line from the code I provided is not of the same length as that in my local file.

推荐答案

不确定为什么要使用批处理文件进行文本处理,但这是一个脚本,可将问题中的第一个文本块转换为第三个文本块:

Not sure why you use batch files for text processing but here's a script that converts the first text block in your question into the third:

@echo off
setlocal enableDelayedExpansion

>output.txt (for /f "skip=2 delims=" %%a in ('find /n /v "" input.txt') do (
    for /f "delims=] tokens=1*" %%b in ("%%a") do (
        set line=%%c
        if "!line!"=="" (
            echo:
        ) else if not "!line:~0,3!"=="C0/" (
            echo ; !line!
            set inblock=
        ) else (
            for /f "tokens=1,2*" %%b in ("!line!") do (
                if "!inblock!"=="" (
                    set inblock=1
                    set label=%%b&set label=!label:/=!
                ) else (
                    set label=
                )
                echo !label!    %%d
            )
        )
    )
))
pause

NB.用!label!之间的文字制表符替换空格.和echo !label! %%d中的%% d.

NB. Replace space(s) with a literal tab character between !label! and %%d in echo !label! %%d.

这篇关于批处理脚本以编辑.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆