18

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

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

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

日志文件示例:

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,

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

4

10 回答 10

18

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
于 2009-02-07T15:47:46.933 回答
12

这个答案结合了现有答案的最佳特征,并增加了一些曲折。

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

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

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

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

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

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

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

最后使用 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%
于 2012-02-12T19:32:57.737 回答
10

您可能应该只找到一个好的实现tail. 但如果你真的坚持使用 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!
于 2009-02-07T06:30:21.740 回答
4

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

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

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

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

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

于 2009-02-07T05:11:42.257 回答
4

如果文件太大,获取行数可能需要很长时间,另一种方法是使用 find 并将其传递给 nowhere 字符串

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

这将导致这样的输出

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

然后你可以像这样使用 for 解析输出

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

然后你可以从总行中减去十行

于 2009-03-16T23:14:58.783 回答
4

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

但是,我在这里找到了很好的答案。为方便起见,复制到这篇文章中。

@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

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

归功于 OP @Peter Mortensen

于 2015-08-14T19:17:07.500 回答
3

使用单个 powershell 命令:

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

适用于 powershell 3.0 及更新版本

于 2015-03-18T13:26:33.093 回答
1

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

如果您不想安装在您的服务器上,您可以将资源包安装在另一台机器上,然后只将 TAIL.EXE 复制到您的服务器上。使用起来非常容易。

C:\> TAIL -10 myfile.txt
于 2012-11-23T15:52:40.280 回答
1

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

call tailhead.bat -file "file.txt" -begin -10
于 2016-01-20T22:13:58.983 回答
0

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

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

# 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.

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.

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.

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.

于 2009-07-13T20:15:11.290 回答