相当于“tail”命令的 Windows
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1295068/
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
Windows equivalent of the 'tail' command
提问by Chris Smith
Is there a way to simulate the *nix tailcommand on the Windows command line? I have a file and I want a way to snip off the first nlines of text. For example:
有没有办法在 Windows 命令行上模拟 *nix tail命令?我有一个文件,我想要一种剪掉前n行文本的方法。例如:
D:\>type file.txt
line one
line two
line three
D:\>*[call to tail]* > result.txt
D:\>type result.txt
line two
line three
采纳答案by Matthew Nizol
No exact equivalent. However there exist a native DOS command "more" that has a +n option that will start outputting the file after the nth line:
没有确切的等价物。然而,存在一个本地 DOS 命令“more”,它有一个 +n 选项,它将在第 n 行之后开始输出文件:
DOS Prompt:
DOS 提示:
C:\>more +2 myfile.txt
The above command will output everything after the first 2 lines.
This is actually the inverse of Unix head:
上面的命令将输出前两行之后的所有内容。
这实际上是 Unix head的倒数:
Unix console:
Unix控制台:
root@server:~$ head -2 myfile.txt
The above command will print only the first 2 lines of the file.
上面的命令将只打印文件的前 2 行。
回答by Amit Portnoy
IFyou have Windows PowerShellinstalled (I think it's included since XP) you can just run from cmd.exe:
如果你安装了Windows PowerShell(我认为它从 XP 开始就包含了)你可以从 cmd.exe 运行:
Head Command:
指挥部:
powershell -command "& {Get-Content *filename* -TotalCount *n*}"
Tail Command:
尾部命令:
powershell -command "& {Get-Content *filename* | Select-Object -last *n*}"
or, directly from PowerShell:
或者,直接从 PowerShell:
Get-Content *filename* -TotalCount *n*
Get-Content *filename* | Select-Object -last *n*
update
更新
PowerShell 3.0 (Windows 8 and higher) added Tail
command with alias Last
.
Head
and First
aliases to TotalCount
were also added.
PowerShell 3.0(Windows 8 及更高版本)添加了Tail
带有 alias 的命令Last
。
Head
并且还添加了First
别名TotalCount
。
So, commands can be re-written as
因此,命令可以重写为
Get-Content *filename* -Head *n*
Get-Content *filename* -Tail *n*
回答by Dan
more /e filename.txt P n
where n = the number of rows to display. Works fast and is exactly like head
command.
其中 n = 要显示的行数。工作速度快,就像head
命令一样。
回答by Sebastian Paaske T?rholm
回答by user2041301
This is a total hack but if it's a huge file that you want to just examine the format, header, etc. and you're looking for a solution you can always just redirect the 'more' output to a new file and CTRL-C quickly. The output rows can't be controlled precisely and you will most likely kill it in the middle of a line of output but it's a cheap way of grabbing a small bit of an otherwise unusable file.
这是一个完全的黑客,但如果它是一个巨大的文件,您只想检查格式,标题等,并且您正在寻找解决方案,您可以随时将“更多”输出重定向到新文件和 CTRL-C迅速地。无法精确控制输出行,您很可能会在输出行的中间杀死它,但这是一种获取少量其他不可用文件的廉价方法。
Ex.
前任。
C:\more test.csv > test.txt
^C C:\more test.txt
line 1
line 2
etc...... C:\
回答by Manuel Alves
Powershell:
电源外壳:
Get-Content C:\logs\result.txt -Tail 10
Get-Content C:\logs\result.txt -wait (monitor the tail)
回答by brianary
Well, this will doit, but it's about as fast as it looks (roughly O(n*m), where n is the number of lines to display and m is the total number of lines in the file):
好吧,这会做到,但它的速度和看起来一样快(大约为 O(n*m),其中 n 是要显示的行数,m 是文件中的总行数):
for /l %l in (1,1,10) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" filename ^| findstr /r "^%l:"') do @echo %b
Where "10" is the number of lines you want to print, and "filename" is the name of the file.
其中“10”是要打印的行数,“filename”是文件名。
回答by Anon
When using more +n that Matt already mentioned, to avoid pauses in long files, try this:
当使用 Matt 已经提到的 more +n 时,为了避免长文件中的暂停,试试这个:
more +1 myfile.txt > con
更多 +1 myfile.txt > con
When you redirect the output from more, it doesn't pause - and here you redirect to the console. You can similarly redirect to some other file like this w/o the pauses of more if that's your desired end result. Use > to redirect to file and overwrite it if it already exists, or >> to append to an existing file. (Can use either to redirect to con.)
当您从 more 重定向输出时,它不会暂停 - 在这里您重定向到控制台。如果这是您想要的最终结果,您可以类似地重定向到这样的其他文件,而无需更多的暂停。使用 > 重定向到文件并在它已经存在时覆盖它,或者使用 >> 附加到现有文件。(可以使用任一重定向到 con。)
回答by brigasnuncamais
回答by Sebas
Get-content -Tail n file.txt
with powershell is the only thing that comes close to tail
in linux.
Get-content -Tail n file.txt
使用 powershell 是唯一接近tail
linux 的东西。
The Get-Content *filename* | Select-Object -last *n*
suggested above loads/parse the whole thing. Needless to say, it was not happy with my 10GB log file... The -Tail
option does start by the end of the file.
在Get-Content *filename* | Select-Object -last *n*
上述建议的加载/解析了整个事情。不用说,它对我的 10GB 日志文件不满意......该-Tail
选项确实在文件末尾开始。