简体   繁体   English

批处理脚本显示文本文件中的 n 到 m 行并保存新的文本文件

[英]batch script to display from n to m lines from a text file and save a new text file

I have found code (see below) but I do not have the ability to encode.我找到了代码(见下文),但我没有编码的能力。 I want to select the desired part (lines) from text file and save in another text file!我想从文本文件中选择所需的部分(行)并保存在另一个文本文件中! Thank you for all your help!谢谢你的帮助!

CODE:代码:

@echo OFF
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (data.txt) do (
set /a LINES=LINES+1
)

:: Print the last 10 lines
set /a LINES=LINES-10
more +%LINES% < data.txt

Note The following reflects your question body and code content only ignoring your question title text.注意以下反映了您的问题正文和代码内容,仅忽略了您的问题标题文本。

Here's a script which will do what your question code does except it outputs to a file.这是一个脚本,它将执行您的问题代码所做的工作,除了输出到文件。

The output file will be written to the current directory and be named, Name -Last X.ext .输出文件将写入当前目录并命名为Name -Last X.ext Where Name and .ext will match the same name and extension as your source file and X will be the number of lines of output.其中Name.ext将匹配与源文件相同的名称和扩展名, X将是输出的行数。

The only changes you would ever need to make would be to the source file on line 3 and the last number of lines to output on line 4.您需要做的唯一更改是更改第 3 行的源文件和第 4 行要输出的最后几行。

@Echo Off

Set "SrcFile=data.txt"
Set "LastNum=10"

Rem If SrcFile doesn't exist or LastNum isn't at least 1 exit the script
If Not Exist "%SrcFile%" Exit/B
If 1%LastNum% Lss 11 Exit/B

Rem Calculate line number for first line of output
Set "#=-%LastNum%"
For /F "Delims=" %%A In ('Find /V /C ""^<"%SrcFile%"') Do Set/A #+=%%A

Rem Re-calculation for files with less lines than LastNum
If %#% Lss 0 Set/A "LastNum+=#, #=0"

Rem Create destination filename for output
For %%A In ("%SrcFile%") Do Set "$=%%~nA-Last%LastNum%%%~xA"

Rem Output to destination file
More +%#% "%SrcFile%">"%$%"

Note that More will change tab s to a sequence of space s请注意, More会将tab更改为空格序列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM