从文本文件中回显第 n 行,其中“n"是命令行参数 [英] Echo the nth line from a text file where 'n' is a command line argument

查看:23
本文介绍了从文本文件中回显第 n 行,其中“n"是命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有数字的简单文本文件,例如:

I have a simple text file with numbers like:

12345
45678
34567
89101

我需要一个批处理来返回这个文件的第 n 行.n 应该取自命令行参数.

I need a batch that will return the nth line from this file. n should be taken from a command line argument.

我对批处理脚本很陌生,所以在此先感谢您的帮助.

I am very new to batch scripting so Thanks in advance for any help on this.

推荐答案

要获取文件来自的第 n 行,您可以使用 more +n (For line1 is n=0).
要拆分文件的其余部分,您可以使用 FOR/F 循环.

To get the file from the nth line you could use more +n (For line1 is n=0).
To split the rest of the file you could use a FOR /F loop.

即使在第 n 行之前有空行,这也有效.
可能需要将 EOL 设置为未使用的字符或换行(默认为 ;)

This works even, if there are empty lines before the nth line.
It could be necessary to set the EOL to an unused character or to linefeed (default is ;)

set "lineNr=%1"
set /a lineNr-=1
for /f "usebackq delims=" %%a in (`more +%lineNr% text.txt`) DO (
  echo %%a
  goto :leave
)
:leave

这篇关于从文本文件中回显第 n 行,其中“n"是命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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