如何逐行读取批处理文件中多余字符的文本文件?允许限制行长.(Windows,批处理脚本) [英] How to read text file line by line which is excessing characters in batch file? Limiting the line length is allowed.(Windows, batch script)

查看:92
本文介绍了如何逐行读取批处理文件中多余字符的文本文件?允许限制行长.(Windows,批处理脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

##test.txt##
First line = 1;*|:12345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345123451234512345
Second line = 5;*|:3215432;*|:21543215432154321543215432154321543215432154321543215432154321543215;*|:543215;*|:5432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321543215432154321

SetLocal EnableDelayedexpansion
for /F "tokens=* delims=" %%a in ('type "test.txt"') do (
            set "Line=%%a"
            echo Line: "!Line!"
)

很显然,以上代码无法读取test.txt中的两行,因为每一行都超出了限制(8191个字符).

Obviously, the above code cannot read 2 lines in test.txt, since each line is over the limit(8191 characters).

通过键入

type "test.txt"

它仍然导致test.txt文件中的两行.但是,只要有2行进入for循环,就无法读取这些行.

It still resulted in the 2 lines as in the test.txt file. However, as soon as 2 lines get in the for loop, the lines cannot be read.

我不需要整个行长,因此我尝试通过删除字符串的多余部分来预处理文本文件.例如,在上面的示例中,将行长度限制为8100.

I do not need the whole length of line, so I tried to preprocess the text file, by deleting excessing part of the string. For example, limit the line length as 8100 in the above example.

1.如何在Windows环境中不使用记事本(任何GUI编辑器)的情况下编辑上述文本文件?

1.How do I edit the above text file without using notepad(any GUI editors) in windows environment?

2.如果我不需要预处理文件,那么避免上述问题的诀窍是什么?

2.If I do not need to preprocess the file, what is the trick to avoid the above issue?

  • 即使我不能放胡萝卜(^),它也可以扩展命令范围.我想要的是将字符串拆分并采用第一部分的方法,但是我不想通过记事本或其他GUI文件编辑器手动进行操作.

推荐答案

您可以使用块读取器,它将每一行拆分成最大为1023个字符的块.

You can use a chunk reader, it splits each line into chunks of a maximum size of 1023 characters, each.

@echo off
setlocal EnableDelayedExpansion

set pos=0
set line=
<long_text.txt (
    FOR /F "tokens=1 delims=:" %%1 in ('findstr /o "^" long_text.txt') DO (
        set new_pos=%%1
        set /a size=new_pos-pos
        set /a "chunks=(size-1+1022) / 1023"
        if defined line echo Line: !line! chunks=!chunks!

        set /a pos=new_pos
        for /L %%# in (1 1 !chunks!) do (
            set "partial="
            set /p partial=     
            if defined partial (
                echo   #!line! chunk %%# -- !partial:~0,10! ... !partial:~-10! 
            )
        )
        set /a line+=1
    )
)

工作原理

外部FOR /F .. findstr /O循环用于通过计算两条线之间的位置差来确定每条线的长度.
行长用于计算要读取整行需要读取多少大块.

How it works

The outer FOR /F .. findstr /O loop is used to determine the length of each line, by calculating the positional difference between two lines.
The line length is used to calculate how many chunks has to be read to fetch the whole line.

set /p读取一行本身(它从<long_text.txt的重定向读取). set /p的内置限制为1023个字符.

A line itself is read by set /p (it reads from the redirection of <long_text.txt). set /p has a build in limit of 1023 characters.

因此set /p被使用了 chunk 次.

要计算一行中使用多少块,必须将行长除以1023,但必须减去1个字符,因为LF不计数(而是CR). +1022是以下事实的结果:必须读取最后一个 complete 块之后的其余字符.
如果该行是1023的倍数(也是一个空行),则最后一个块可以为空.

To calculate how many chunks are used for a single line, the line length has to be divided by 1023, but 1 character has to be subtracted, because the LF doesn't count (but the CR). The +1022 is a result of the fact, that the remaining characters after the last complete chunk has to be read, too.
The last chunk can be empty, if the line is a multiple of 1023 (also an empty line).

剩下的唯一点是最后一行.
此技术不会读取最后一行,但在前面添加一个空行很容易.

The only remaining point is the last line.
The last line will not be read by this technique, but it's easy enough to append one empty line before.

这篇关于如何逐行读取批处理文件中多余字符的文本文件?允许限制行长.(Windows,批处理脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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