CMD.EXE批处理脚本,用于显示txt文件中的最后10行

时间:2021-10-17 02:32:14

Any ideas how to echo or type the last 10 lines of a txt file?

有关如何回显或输入txt文件的最后10行的任何想法?

I'm running a server change log script to prompt admins to state what they're doing, so we can track changes. I'm trying to get the script to show the last 10 entries or so to give an idea of what's been happening recently. I've found a script that deals with the last line, as shown below, but can't figure out what to change in it to display the last 10 lines. Script:

我正在运行服务器更改日志脚本以提示管理员说明他们正在做什么,因此我们可以跟踪更改。我正在尝试让脚本显示最后10个条目,以便了解最近发生的事情。我找到了一个处理最后一行的脚本,如下所示,但无法弄清楚要在其中更改哪些内容以显示最后10行。脚本:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\log09.txt) do (
set var=%%a
)
echo !var!

Example of log file:

日志文件示例:

06/02/2009, 12:22,Remote=Workstation-9,Local=,
mdb,bouncing box after updates,CAS-08754,
=================
07/02/2009, 2:38,Remote=,Local=SERVER1,
mdb,just finished ghosting c drive,CAS-08776,
=================
07/02/2009, 3:09,Remote=,Local=SERVER1,
mdb,audit of server,CAS-08776,

Any thoughts? The script works great, just need it to pipe more lines to the screen.

有什么想法吗?该脚本运行良好,只需要将更多行传输到屏幕。

10 个解决方案

#1


Hopefully this will save Joel's eyes :)

希望这会拯救乔尔的眼睛:)

@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 (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txt

#2


This answer combines the best features of already existing answers, and adds a few twists.

这个答案结合了现有答案的最佳功能,并添加了一些曲折。

The solution is a simple batch implementation of the tail command.

解决方案是tail命令的简单批处理实现。

The first argument is the file name (possibly with path information - be sure to enclose in quotes if any portion of path contains spaces or other problematic characters).

第一个参数是文件名(可能带有路径信息 - 如果路径的任何部分包含空格或其他有问题的字符,请确保用引号括起来)。

The second argument is the number of lines to print.

第二个参数是要打印的行数。

Finally any of the standard MORE options can be appended: /E /C /P /S /Tn. (See MORE /? for more information).

最后,可以附加任何标准的MORE选项:/ E / C / P / S / Tn。 (有关更多信息,请参阅更多/?)。

Additionally the /N (no pause) option can be specified to cause the output to be printed continuosly without pausing.

此外,可以指定/ N(无暂停)选项以使输出连续打印而不会暂停。

The solution first uses FIND to quickly count the number of lines. The file is passed in via redirected input instead of using a filename argument in order to eliminate the printout of the filename in the FIND output.

解决方案首先使用FIND快速计算行数。该文件通过重定向输入而不是使用filename参数传递,以消除FIND输出中文件名的打印输出。

The number of lines to skip is computed with SET /A, but then it resets the number to 0 if it is less than 0.

要跳过的行数用SET / A计算,但如果小于0则将数字重置为0。

Finally uses MORE to print out the desired lines after skipping the unwanted lines. MORE will pause after each screen's worth of lines unless the output is redirected to a file or piped to another command. The /N option avoids the pauses by piping the MORE output to FINDSTR with a regex that matches all lines. It is important to use FINDSTR instead of FIND because FIND can truncate long lines.

最后使用MORE在跳过不需要的行后打印出所需的行。除非将输出重定向到文件或通过管道传输到另一个命令,否则MORE将在每个屏幕的行数之后暂停。 / N选项通过使用匹配所有行的正则表达式将MORE输出传递给FINDSTR来避免暂停。使用FINDSTR而不是FIND很重要,因为FIND可以截断长行。

:: tail.bat File Num [/N|/E|/C|/P|/S|/Tn]...
::
::   Prints the last Num lines of text file File.
::
::   The output will pause after filling the screen unless the /N option
::   is specified
::
::   The standard MORE options /E /C /P /S /Tn can be specified.
::   See MORE /? for more information
::
@echo OFF
setlocal
set file=%1
set "cnt=%~2"
shift /1
shift /1
set "options="
set "noPause="
:parseOptions
if "%~1" neq "" (
  if /i "%~1" equ "/N" (set noPause=^| findstr "^") else set options=%options% %~1
  shift /1
  goto :parseOptions
)
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
set /a "skip-=%cnt%"
if %skip% lss 0 set skip=0
more +%skip% %options% %file% %noPause%

#3


You should probably just find a good implementation of tail. But if you really really insist on using CMD batch files and want to run on any NT machine unmolested, this will work:

您应该只是找到一个很好的尾部实现。但是,如果你真的坚持使用CMD批处理文件并希望在任何NT机器上运行,那么这将是有效的:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\tmp\foo.txt) do (
set var9=!var8!
set var8=!var7!
set var7=!var6!
set var6=!var5!
set var5=!var4!
set var4=!var3!
set var3=!var2!
set var2=!var1!
set var1=!var!
set var=%%a
)
echo !var9!
echo !var8!
echo !var7!
echo !var6!
echo !var5!
echo !var4!
echo !var3!
echo !var2!
echo !var1!
echo !var!

#4


There are several windows implementations of the tail command. It should be exactly what you want.

tail命令有几个windows实现。它应该是你想要的。

This one sounds particularly good: http://malektips.com/xp_dos_0001.html

这听起来特别好:http://malektips.com/xp_dos_0001.html

They range from real-time monitoring to the last x lines of the file.

它们的范围从实时监控到文件的最后x行。

Edit: I noticed that the included link is to a package It should work, but here are some more versions:

编辑:我注意到包含的链接是一个包它应该工作,但这里有一些更多的版本:

http://www.lostinthebox.com/viewtopic.php?f=5&t=3801 http://sourceforge.net/projects/tailforwin32

#5


If file is too large it can take too long to get count of lines another way is to use find and pass it a nowhere string

如果文件太大,可能需要太长时间来计算行数,另一种方法是使用find并将其传递给无处字符串

$find /v /c "%%$%!" yourtextfile.txt

this would result an output like this

这会产生这样的输出

$---------- yourtextfile.txt: 140

then you can parse output using for like this

那么你可以使用像这样解析输出

$for /f "tokens=3" %i in ('find /v /c "%%$%!" tt.txt') do set countoflines=%i

then you can substract ten lines from the total lines

那么你可以从总线中减去十行

#6


After trying all of the answers I found on this page none of them worked on my file with 15539 lines.

在尝试了我在这个页面上找到的所有答案后,他们都没有使用15539行处理我的文件。

However I found the answer here to work great. Copied into this post for convenience.

但是我发现这里的答案非常好。为方便起见,复制到这篇文章。

@echo off
for /f %%i in ('find /v /c "" ^< C:\path\to\textfile.txt') do set /a lines=%%i
set /a startLine=%lines% - 10
more /e +%startLine% C:\path\to\textfile.txt

This code will print the last 10 lines in the "C:\path\to\textfile.txt" file.

此代码将打印“C:\ path \ to \ textfile.txt”文件中的最后10行。

Credit goes to OP @Peter Mortensen

归功于OP @Peter Mortensen

#7


using a single powershell command:

使用单个powershell命令:

powershell -nologo "& "Get-Content -Path c:\logFile.log -Tail 10"

applies to powershell 3.0 and newer

适用于powershell 3.0及更新版本

#8


I agree with "You should use TAIL" answer. But it does not come by default on Windows. I suggest you download the "Windows 2003 Resource Kit" It works on XP/W2003 and more.

我同意“你应该使用TAIL”的答案。但它在Windows上默认不会出现。我建议你下载“Windows 2003资源工具包”它适用于XP / W2003等。

If you don't want to install on your server, you can install the resource kit on another machine and copy only TAIL.EXE to your server. Usage is sooooo much easier.

如果您不想在服务器上安装,可以在另一台计算机上安装资源工具包,并仅将TAIL.EXE复制到您的服务器。用法太容易了。

C:\> TAIL -10 myfile.txt

#9


Here's a utility written in pure batch that can show a lines of file within a given range.To show the last lines use (here the script is named tailhead.bat):

这是一个用纯批处理编写的实用程序,它可以显示给定范围内的一行文件。要显示最后一行使用(这里的脚本名为tailhead.bat):

call tailhead.bat -file "file.txt" -begin -10

#10


Any ideas how to echo or type the last 10 lines of a txt file?

有关如何回显或输入txt文件的最后10行的任何想法?

The following 3-liner script will list the last n lines from input file. n and file name/path are passed as input arguments.

以下3行脚本将列出输入文件中的最后n行。 n和文件名/路径作为输入参数传递。

# Script last.txt
var str file, content ; var int n, count
cat $file > $content ; set count = { len -e $content } - $n
stex -e ("["+makestr(int($count))) $content

The script is in biterscripting. To use, download biterscripting from http://www.biterscripting.com , save this script as C:\Scripts\last.txt, start biterscripting, enter the following command.

该脚本是biterscripting。要使用,请从http://www.biterscripting.com下载biterscripting,将此脚本保存为C:\ Scripts \ last.txt,启动biterscripting,输入以下命令。

script last.txt file("c:\log09.txt") n(10)

The above will list last 10 lines from file c:\log09.txt. To list last 20 lines from the same file, use the following command.

以上将列出文件c:\ log09.txt中的最后10行。要列出同一文件中的最后20行,请使用以下命令。

script last.txt file("c:\log09.txt") n(20)

To list last 30 lines from a different file C:\folder1\somefile.log, use the following command.

要列出来自不同文件C:\ folder1 \ somefile.log的最后30行,请使用以下命令。

script last.txt file("C:\folder1\somefile.log") n(30)

I wrote the script in a fairly generic way, so it can be used in various ways. Feel free to translate into another scripting/batch language.

我以相当通用的方式编写脚本,因此可以以各种方式使用它。随意翻译成另一种脚本/批处理语言。

#1


Hopefully this will save Joel's eyes :)

希望这会拯救乔尔的眼睛:)

@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 (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txt

#2


This answer combines the best features of already existing answers, and adds a few twists.

这个答案结合了现有答案的最佳功能,并添加了一些曲折。

The solution is a simple batch implementation of the tail command.

解决方案是tail命令的简单批处理实现。

The first argument is the file name (possibly with path information - be sure to enclose in quotes if any portion of path contains spaces or other problematic characters).

第一个参数是文件名(可能带有路径信息 - 如果路径的任何部分包含空格或其他有问题的字符,请确保用引号括起来)。

The second argument is the number of lines to print.

第二个参数是要打印的行数。

Finally any of the standard MORE options can be appended: /E /C /P /S /Tn. (See MORE /? for more information).

最后,可以附加任何标准的MORE选项:/ E / C / P / S / Tn。 (有关更多信息,请参阅更多/?)。

Additionally the /N (no pause) option can be specified to cause the output to be printed continuosly without pausing.

此外,可以指定/ N(无暂停)选项以使输出连续打印而不会暂停。

The solution first uses FIND to quickly count the number of lines. The file is passed in via redirected input instead of using a filename argument in order to eliminate the printout of the filename in the FIND output.

解决方案首先使用FIND快速计算行数。该文件通过重定向输入而不是使用filename参数传递,以消除FIND输出中文件名的打印输出。

The number of lines to skip is computed with SET /A, but then it resets the number to 0 if it is less than 0.

要跳过的行数用SET / A计算,但如果小于0则将数字重置为0。

Finally uses MORE to print out the desired lines after skipping the unwanted lines. MORE will pause after each screen's worth of lines unless the output is redirected to a file or piped to another command. The /N option avoids the pauses by piping the MORE output to FINDSTR with a regex that matches all lines. It is important to use FINDSTR instead of FIND because FIND can truncate long lines.

最后使用MORE在跳过不需要的行后打印出所需的行。除非将输出重定向到文件或通过管道传输到另一个命令,否则MORE将在每个屏幕的行数之后暂停。 / N选项通过使用匹配所有行的正则表达式将MORE输出传递给FINDSTR来避免暂停。使用FINDSTR而不是FIND很重要,因为FIND可以截断长行。

:: tail.bat File Num [/N|/E|/C|/P|/S|/Tn]...
::
::   Prints the last Num lines of text file File.
::
::   The output will pause after filling the screen unless the /N option
::   is specified
::
::   The standard MORE options /E /C /P /S /Tn can be specified.
::   See MORE /? for more information
::
@echo OFF
setlocal
set file=%1
set "cnt=%~2"
shift /1
shift /1
set "options="
set "noPause="
:parseOptions
if "%~1" neq "" (
  if /i "%~1" equ "/N" (set noPause=^| findstr "^") else set options=%options% %~1
  shift /1
  goto :parseOptions
)
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
set /a "skip-=%cnt%"
if %skip% lss 0 set skip=0
more +%skip% %options% %file% %noPause%

#3


You should probably just find a good implementation of tail. But if you really really insist on using CMD batch files and want to run on any NT machine unmolested, this will work:

您应该只是找到一个很好的尾部实现。但是,如果你真的坚持使用CMD批处理文件并希望在任何NT机器上运行,那么这将是有效的:

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\tmp\foo.txt) do (
set var9=!var8!
set var8=!var7!
set var7=!var6!
set var6=!var5!
set var5=!var4!
set var4=!var3!
set var3=!var2!
set var2=!var1!
set var1=!var!
set var=%%a
)
echo !var9!
echo !var8!
echo !var7!
echo !var6!
echo !var5!
echo !var4!
echo !var3!
echo !var2!
echo !var1!
echo !var!

#4


There are several windows implementations of the tail command. It should be exactly what you want.

tail命令有几个windows实现。它应该是你想要的。

This one sounds particularly good: http://malektips.com/xp_dos_0001.html

这听起来特别好:http://malektips.com/xp_dos_0001.html

They range from real-time monitoring to the last x lines of the file.

它们的范围从实时监控到文件的最后x行。

Edit: I noticed that the included link is to a package It should work, but here are some more versions:

编辑:我注意到包含的链接是一个包它应该工作,但这里有一些更多的版本:

http://www.lostinthebox.com/viewtopic.php?f=5&t=3801 http://sourceforge.net/projects/tailforwin32

#5


If file is too large it can take too long to get count of lines another way is to use find and pass it a nowhere string

如果文件太大,可能需要太长时间来计算行数,另一种方法是使用find并将其传递给无处字符串

$find /v /c "%%$%!" yourtextfile.txt

this would result an output like this

这会产生这样的输出

$---------- yourtextfile.txt: 140

then you can parse output using for like this

那么你可以使用像这样解析输出

$for /f "tokens=3" %i in ('find /v /c "%%$%!" tt.txt') do set countoflines=%i

then you can substract ten lines from the total lines

那么你可以从总线中减去十行

#6


After trying all of the answers I found on this page none of them worked on my file with 15539 lines.

在尝试了我在这个页面上找到的所有答案后,他们都没有使用15539行处理我的文件。

However I found the answer here to work great. Copied into this post for convenience.

但是我发现这里的答案非常好。为方便起见,复制到这篇文章。

@echo off
for /f %%i in ('find /v /c "" ^< C:\path\to\textfile.txt') do set /a lines=%%i
set /a startLine=%lines% - 10
more /e +%startLine% C:\path\to\textfile.txt

This code will print the last 10 lines in the "C:\path\to\textfile.txt" file.

此代码将打印“C:\ path \ to \ textfile.txt”文件中的最后10行。

Credit goes to OP @Peter Mortensen

归功于OP @Peter Mortensen

#7


using a single powershell command:

使用单个powershell命令:

powershell -nologo "& "Get-Content -Path c:\logFile.log -Tail 10"

applies to powershell 3.0 and newer

适用于powershell 3.0及更新版本

#8


I agree with "You should use TAIL" answer. But it does not come by default on Windows. I suggest you download the "Windows 2003 Resource Kit" It works on XP/W2003 and more.

我同意“你应该使用TAIL”的答案。但它在Windows上默认不会出现。我建议你下载“Windows 2003资源工具包”它适用于XP / W2003等。

If you don't want to install on your server, you can install the resource kit on another machine and copy only TAIL.EXE to your server. Usage is sooooo much easier.

如果您不想在服务器上安装,可以在另一台计算机上安装资源工具包,并仅将TAIL.EXE复制到您的服务器。用法太容易了。

C:\> TAIL -10 myfile.txt

#9


Here's a utility written in pure batch that can show a lines of file within a given range.To show the last lines use (here the script is named tailhead.bat):

这是一个用纯批处理编写的实用程序,它可以显示给定范围内的一行文件。要显示最后一行使用(这里的脚本名为tailhead.bat):

call tailhead.bat -file "file.txt" -begin -10

#10


Any ideas how to echo or type the last 10 lines of a txt file?

有关如何回显或输入txt文件的最后10行的任何想法?

The following 3-liner script will list the last n lines from input file. n and file name/path are passed as input arguments.

以下3行脚本将列出输入文件中的最后n行。 n和文件名/路径作为输入参数传递。

# Script last.txt
var str file, content ; var int n, count
cat $file > $content ; set count = { len -e $content } - $n
stex -e ("["+makestr(int($count))) $content

The script is in biterscripting. To use, download biterscripting from http://www.biterscripting.com , save this script as C:\Scripts\last.txt, start biterscripting, enter the following command.

该脚本是biterscripting。要使用,请从http://www.biterscripting.com下载biterscripting,将此脚本保存为C:\ Scripts \ last.txt,启动biterscripting,输入以下命令。

script last.txt file("c:\log09.txt") n(10)

The above will list last 10 lines from file c:\log09.txt. To list last 20 lines from the same file, use the following command.

以上将列出文件c:\ log09.txt中的最后10行。要列出同一文件中的最后20行,请使用以下命令。

script last.txt file("c:\log09.txt") n(20)

To list last 30 lines from a different file C:\folder1\somefile.log, use the following command.

要列出来自不同文件C:\ folder1 \ somefile.log的最后30行,请使用以下命令。

script last.txt file("C:\folder1\somefile.log") n(30)

I wrote the script in a fairly generic way, so it can be used in various ways. Feel free to translate into another scripting/batch language.

我以相当通用的方式编写脚本,因此可以以各种方式使用它。随意翻译成另一种脚本/批处理语言。