windows 如何在 Powershell 中执行 head、tail、more、less、sed 的操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9682024/
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
How to do what head, tail, more, less, sed do in Powershell?
提问by Yue Zhang
On windows, using Powershell, what are the equivalent commands to linux's head
, tail
, more
, less
and sed
?
在Windows上,使用PowerShell,什么是等效命令到Linux的head
,tail
,more
,less
和sed
?
回答by Joey
Get-Content
(alias: gc
) is your usual option for reading a text file. You can then filter further:
Get-Content
(别名:)gc
是您阅读文本文件的常用选项。然后您可以进一步过滤:
gc log.txt | select -first 10 # head
gc -TotalCount 10 log.txt # also head
gc log.txt | select -last 10 # tail
gc -Tail 10 log.txt # also tail (since PSv3), also much faster than above option
gc log.txt | more # or less if you have it installed
gc log.txt | %{ $_ -replace '\d+', '(gc log.txt -head 10
gc log.txt -tail 10
gc log.txt -tail 10 -wait # equivalent to tail -f
)' } # sed
This works well enough for small files, larger ones (more than a few MiB) are probably a bit slow.
这对于小文件来说效果很好,大文件(超过几个 MiB)可能有点慢。
The PowerShell Community Extensionsinclude some cmdlets for specialised file stuff (e.g. Get-FileTail).
在PowerShell的社区扩展包括专门文件的东西(如GET-FileTail)某些cmdlet。
回答by wisbucky
Here are the built-in ways to do head
and tail
. Don't use pipes because if you have a large file, it will be extremely slow. Using these built-in options will be extremely fast even for huge files.
下面是内置的方法来做head
和tail
。不要使用管道,因为如果你有一个大文件,它会非常慢。即使对于大文件,使用这些内置选项也会非常快。
gc -TotalCount 25 C:\scripts\logs\robocopy_report.txt
回答by Richard
more.exe
exists on Windows, ports of less
are easily found (and the PowerShell Community Extensions, PSCX, includes one).
more.exe
存在于 Windows 上,less
很容易找到 的端口(PowerShell 社区扩展,PSCX,包括一个)。
PowerShell doesn't really provide any alternative to separate programs for either, but for structured data Out-Grid
can be helpful.
PowerShell 并没有真正为两者提供任何单独程序的替代方案,但对于结构化数据Out-Grid
可能会有所帮助。
Head
and Tail
can both be emulated with Select-Object
using the -First
and -Last
parameters respectively.
Head
和Tail
都可以分别Select-Object
使用-First
和-Last
参数进行模拟。
Sed
functions are all available but structured rather differently. The filtering options are available in Where-Object
(or via Foreach-Object
and some state for ranges). Other, transforming, operations can be done with Select-Object
and Foreach-Object
.
Sed
功能都可用,但结构却大不相同。过滤选项在Where-Object
(或通过Foreach-Object
和范围的某些状态)中可用。其他的转换操作可以用Select-Object
和来完成Foreach-Object
。
However as PowerShell passes (.NET) objects – with all their typed structure, eg. dates remain DateTime
instances – rather than just strings, which each command needs to parse itself, much of sed
and other such programs are redundant.
然而,当 PowerShell 传递 (.NET) 对象时——具有所有类型的结构,例如。日期仍然是DateTime
实例——而不仅仅是字符串,每个命令都需要解析它自己,很多sed
和其他这样的程序都是多余的。
回答by Patrick Burwell
"-TotalCount" in this instance responds exactly like "-head". You have to use -TotalCount or -head to run the command like that. But -TotalCount is misleading - it does not work in ACTUALLY giving you ANY counts...
在这种情况下,“-TotalCount”的响应与“-head”完全一样。您必须使用 -TotalCount 或 -head 来运行这样的命令。但是 -TotalCount 具有误导性 - 它实际上无法为您提供任何计数......
gc -head 25 C:\scripts\logs\robocopy_report.txt
The above script, tested in PS 5.1 is the SAME response as below...
上面的脚本,在 PS 5.1 中测试是相同的响应如下...
gc log.txt -ReadCount 5 | %{$_;throw "pipeline end!"} # head
gc log.txt | %{$num=0;}{$num++;"$num $_"} # cat -n
gc log.txt | %{$num=0;}{$num++; if($num -gt 2 -and $num -lt 7){"$num $_"}} # sed
So then just use '-head 25" already!
那么就使用 '-head 25" 吧!
回答by What Would Be Cool
If you need to query large (or small) log files on Windows, the best tool I have found is Microsoft's free Log Parser 2.2. You can call it from PowerShell if you want and it will do all the heavy lifting for you, and very fast too.
如果您需要在 Windows 上查询大(或小)日志文件,我找到的最好的工具是 Microsoft 的免费Log Parser 2.2。如果需要,您可以从 PowerShell 调用它,它会为您完成所有繁重的工作,而且速度也非常快。
回答by Yue Zhang
I got some better solutions:
我得到了一些更好的解决方案:
$Push_Pop = $ErrorActionPreference #Suppresses errors
$ErrorActionPreference = “SilentlyContinue” #Suppresses errors
#Script
#gc .\output\*.csv -ReadCount 5 | %{$_;throw "pipeline end!"} # head
#gc .\output\*.csv | %{$num=0;}{$num++;"$num $_"} # cat -n
gc .\output\*.csv | %{$num=0;}{$num++; if($num -gt 2 -and $num -lt 7){"$num $_"}} # sed
#End Script
$ErrorActionPreference = $Push_Pop #Suppresses errors
回答by Patrick Burwell
You don't get all the errors with the pushpop code BTW, your code only works with the "sed" option. All the rest ignores anything but gc and path.
顺便说一句,pushpop 代码不会出现所有错误,您的代码仅适用于“sed”选项。所有其余的都忽略了 gc 和 path 之外的任何内容。