与 Unix tail 命令等效的 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/187587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:30:54  来源:igfitidea点击:

A Windows equivalent of the Unix tail command

windowstail

提问by Liam

I'm looking for the equivalent of the Unix 'tail' command that will allow me to watch the output of a log file while it is being written to.

我正在寻找与 Unix 'tail' 命令等效的命令,它允许我在写入日志文件时观察它的输出。

采纳答案by Ryan Duffield

I'd suggest installing something like GNU Utilities for Win32. It has most favourites, including tail.

我建议安装类似GNU Utilities for Win32 的东西。它有最喜欢的,包括尾巴。

回答by Alex

If you use PowerShell then this works:

如果您使用 PowerShell,那么这有效:

Get-Content filenamehere -Wait -Tail 30

Posting Stefan's comment from below, so people don't miss it

从下面发布 Stefan 的评论,这样人们就不会错过它

PowerShell 3 introduces a -Tail parameter to include only the last x lines

PowerShell 3 引入了 -Tail 参数以仅包含最后 x 行

回答by Instantsoup

I've always used Baretailfor tailing in Windows. It's free and pretty nice.

我一直使用Baretail在 Windows 中进行拖尾。它是免费的,而且非常好。

Edit: for a better description of Baretail see this question

编辑:为了更好地描述 Baretail,请参阅此问题

回答by Quentin

You can get tail as part of Cygwin.

你可以得到 tail 作为Cygwin 的一部分。

回答by Quentin

Anybody interested in a DOSCMD tail using batch commands (see below).

任何对使用批处理命令的DOSCMD 尾部感兴趣的人(见下文)。

It's not prefect, and lines sometime repeat.

这不是完美的,有时台词会重复。

Usage: tail.bat -d tail.bat -f -f

用法:tail.bat -d tail.bat -f -f

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
rem tail.bat -d <lines> <file>
rem tail.bat -f <file>

rem ****** MAIN ******
IF "%1"=="-d" GOTO displayfile
IF "%1"=="-f" GOTO followfile

GOTO end

rem ************
rem Show Last n lines of file
rem ************

:displayfile
SET skiplines=%2
SET sourcefile=%3

rem *** Get the current line count of file ***
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET find_lc=%%l)

rem *** Calculate the lines to skip
SET /A skiplines=%find_lc%-!skiplines!

rem *** Display to screen line needed
more +%skiplines% %sourcefile%

GOTO end

rem ************
rem Show Last n lines of file & follow output
rem ************

:followfile
SET skiplines=0
SET findend_lc=0
SET sourcefile=%2

:followloop
rem *** Get the current line count of file ***
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET find_lc=%%l)
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET findend_lc=%%l)

rem *** Calculate the lines to skip
SET /A skiplines=%findend_lc%-%find_lc%
SET /A skiplines=%find_lc%-%skiplines%

rem *** Display to screen line when file updated
more +%skiplines% %sourcefile%

goto followloop

:end

回答by gz.

There are quite a number of options, however all of them have flaws with more advanced features.

有很多选项,但是所有选项都存在缺陷,具有更高级的功能。

  • GnuWin32 tailis buggy (αβγ) - things like -f just plain don't work.

  • UnxUtils tailseems better (-f works, but --pid seems not to, -n but not --lines=n fails with -f), but appears to be a dead project.

  • Cygwinis a big ugly mush, could perhaps just use the DLL and coreutils package- but still has problems like --pid not working with native win32 processes.

  • GnuWin32 尾部有问题(α β γ) - 像 -f 这样的东西根本行不通。

  • UnxUtils tail似乎更好(-f 有效,但 --pid 似乎无效,-n 但不是 --lines=n 与 -f 失败),但似乎是一个死项目。

  • Cygwin是一个很大的丑陋糊状物,也许可以只使用 DLL 和coreutils 包- 但仍然存在诸如 --pid 不能与本机 win32 进程一起工作的问题。

回答by Jake

I've used Tail For Windows. Certainly not as elegant as using

我用过Tail For Windows。当然不如使用优雅

tail
但是,您使用的是 Windows。;)

回答by OscarRyz

With Windows PowerShell you can use:

借助 Windows PowerShell,您可以使用:

Get-Content <file> -Wait

回答by Grzegorz Gralak

I haven't seen Log Expert anywhere among answers here.

我在这里的答案中没有看到日志专家。

It's customizable and is quite good for going around log files. So far it's the best Windows graphical log viewer for me.

它是可定制的,非常适合处理日志文件。到目前为止,它对我来说是最好的 Windows 图形日志查看器。

Unfortunately, this software is no longer available. You can read about it on archive.org.

不幸的是,该软件不再可用。您可以在archive.org上阅读有关它的信息。

回答by Philibert Perusse

If you do not want to install anything at all you can "build your own" batch file that does the job from standard Windows commands. Here are some pointers as to how to do it.

如果您根本不想安装任何东西,您可以“构建您自己的”批处理文件,通过标准的 Windows 命令来完成这项工作。以下是有关如何执行此操作的一些提示。

1) Using find /c /v "" yourinput.file, get the number of lines in your input file. The output is something like:

1) 使用find /c /v "" yourinput.file,获取输入文件中的行数。输出类似于:

---------- T.TXT: 15

2) Using for /f, parse this output to get the number 15.

2) 使用for /f,解析此输出以获得数字 15。

3) Using set /a, calculate the number of head lines that needs to be skipped

3) 使用set /a,计算需要跳过的标题行数

4) Using for /f "skip=n"skip the head lines and echo/process the tail lines.

4) 使用for /f "skip=n"跳过头行并回显/处理尾行。

If I find the time, I will build such a batch file and post it back here.

如果我有时间,我会建立一个这样的批处理文件并将其发回这里。

EDIT: tail.bat

编辑: tail.bat

REM tail.bat
REM
REM Usage: tail.bat <file> <number-of-lines> 
REM
REM Examples: tail.bat myfile.txt 10
REM           tail.bat "C:\My File\With\Spaces.txt" 10

@ECHO OFF
for /f "tokens=2-3 delims=:" %%f in ('find /c /v "" %1') do (
    for %%F in (%%f %%g) do set nbLines=%%F )
set /a nbSkippedLines=%nbLines%-%2
for /f "usebackq skip=%nbSkippedLines% delims=" %%d in (%1) do echo %%d