如何在Powershell中做头,尾,多,少,多做的事情?

时间:2021-03-10 16:04:42

I need these commands to check log file on Windows, but I don't install any program, and I like Powershell with Windows.

我需要这些命令来检查Windows上的日志文件,但我没有安装任何程序,我喜欢使用Windows的Powershell。

5 个解决方案

#1


104  

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+', '($0)' }         # sed

This works well enough for small files, larger ones (more than a few MiB) are probably a bit slow.

这适用于小文件,较大的文件(超过几个MiB)可能有点慢。

The PowerShell Community Extensions include some cmdlets for specialised file stuff (e.g. Get-FileTail).

PowerShell社区扩展包括一些用于专门文件内容的cmdlet(例如Get-FileTail)。

#2


22  

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.

这是内置的头尾方式。不要使用管道,因为如果你有一个大文件,它将非常慢。即使对于大型文件,使用这些内置选项也会非常快。

gc log.txt -head 10 
gc log.txt -tail 10
gc log.txt -tail 10 -wait # equivalent to tail -f

#3


6  

more.exe exists on Windows, ports of less are easily found (and the PowerShell Community Extensions, PSCX, includes one).

Windows上存在more.exe,很容易找到较少的端口(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都可以使用-First和-Last参数分别使用Select-Object进行模拟。

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和其他此类程序的大部分都是多余的。

#4


1  

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中调用它,它会为你完成所有繁重的任务,而且速度非常快。

#5


0  

I got some better solutions:

我有一些更好的解决方案:

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

#1


104  

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+', '($0)' }         # sed

This works well enough for small files, larger ones (more than a few MiB) are probably a bit slow.

这适用于小文件,较大的文件(超过几个MiB)可能有点慢。

The PowerShell Community Extensions include some cmdlets for specialised file stuff (e.g. Get-FileTail).

PowerShell社区扩展包括一些用于专门文件内容的cmdlet(例如Get-FileTail)。

#2


22  

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.

这是内置的头尾方式。不要使用管道,因为如果你有一个大文件,它将非常慢。即使对于大型文件,使用这些内置选项也会非常快。

gc log.txt -head 10 
gc log.txt -tail 10
gc log.txt -tail 10 -wait # equivalent to tail -f

#3


6  

more.exe exists on Windows, ports of less are easily found (and the PowerShell Community Extensions, PSCX, includes one).

Windows上存在more.exe,很容易找到较少的端口(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都可以使用-First和-Last参数分别使用Select-Object进行模拟。

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和其他此类程序的大部分都是多余的。

#4


1  

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中调用它,它会为你完成所有繁重的任务,而且速度非常快。

#5


0  

I got some better solutions:

我有一些更好的解决方案:

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