實現 Unix Tail 命令的 Windows PowerShell 命令

Migel Hewage Nimesha 2023年12月11日
  1. Win 32 中的 tail 實現 tail Unix 命令功能
  2. Windows PowerShell 配合 Get-Content 實現 tail Unix 命令功能
  3. Windows PowerShell 配合 Cat 實現 tail Unix 命令功能
實現 Unix Tail 命令的 Windows PowerShell 命令

Unix 作業系統包含作業系統命令 tailtail 程式的目的是顯示文字檔案/管道資料的尾部。

預期的輸出是檔案的最後幾行。行數可以變化。同一命令可以同時用於多個檔案以列印標準輸出。

除了 Unix,tail 命令還支援類 Unix 系統和 FreeDOS 和 MSX-DOS 系統。

在 Windows PowerShell(跨平臺應用程式的 Windows 版本)中,可以執行命令列 shell 命令以實現不同功能的自動化。然而,這裡使用的命令不同於類 Unix 系統中使用的標準指令碼命令。

在 Windows PowerShell 中執行 tail 類似功能有幾種可接受的方法。

Win 32 中的 tail 實現 tail Unix 命令功能

實現此功能的常用方法之一是使用 Windows PowerShell 的 tail-f 程式; Win32 的尾部。它跟蹤任何檔案更改並允許使用者實時跟蹤更改。但是,它要求使用者完全使用另一個程式,可通過以下連結獲得:https://tailforwin32.sourceforge.net。因此,對於正在為 Windows 系統搜尋特定於 PowerShell 的替代方案的使用者來說,它不是一個合適的解決方案。

但是,也有一些可接受的基於 PowerShell 的解決方案。

Windows PowerShell 配合 Get-Content 實現 tail Unix 命令功能

一種最有效的方法是使用 Get-Content。它後面是 -Tail n,n 是你需要作為輸出獲得的行數。

在早期版本的 Windows Power shell V1 和 V2 中,此功能是通過 Get-Content 命令和 -Wait 命令實現的。如果檔案實時更改,則使用 Wait 命令以便跟蹤任何更改。

Get-Content .\DummyLogFile.txt

這裡的輸出將獲得整個檔案的內容,如下所示。它從日誌檔案的第一行開始,一直持續到最後。

2021-12-31 14:49:47, Info                  CBS    TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info                  CBS    TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info                  CBS    Startup processing thread terminated normally
2021-12-31 14:49:47, Info                  CBS    TI: Startup Processing completes, release startup processing lock.
.
.

然而,當與 - Wait 一起使用時,可以跟蹤檔案尾部的實時更改。

Get-Content .\DummyLogFile.txt -Wait

在這裡,輸出將包含所有檔案資料並等待下面的更改。

2021-12-31 14:49:47, Info                  CBS    TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info                  CBS    TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info                  CBS    Startup processing thread terminated normally
2021-12-31 14:49:47, Info                  CBS    TI: Startup Processing completes, release startup processing lock.
_

後來在 PowerShell V2 之後的版本中,V3 支援 -Tail 關鍵字。因此,你只能預覽所需的最後幾行,而不是獲取整個檔案。

 Get-Content .\DummyLogFile.txt -Tail 4

給出的輸出是給定文字的最後四行。4 是 n 的一個變數值,在 tail 命令之後包括下面的尾行數。

C:\Users>  Get-Content .\DummyLogFile.txt -Tail 4
2022-01-06 08:58:10, Info                  CBS    Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info                  CBS    Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info                  CBS    Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info                  CBS    Ending TrustedInstaller finalization.
PS C:\Users>

當使用者需要實時資料時,例如日誌檔案、不斷變化的檔案,可以新增 -Wait 命令。新增新的最後一行時,輸出會更新以列印任何新行。

PS C:\Users> Get-Content .\DummyLogFile.txt -Wait -Tail 4

輸出將等待更改。

Get-Content .\DummyLogFile.txt -Wait -Tail 4
2022-01-06 08:58:10, Info                  CBS    Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info                  CBS    Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info                  CBS    Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info                  CBS    Ending TrustedInstaller finalization.

但是,當你使用具有較大尾部值的 -wait 時,你會讓系統在一個不斷變化的檔案中等待,例如日誌檔案。這會導致發生大量記憶體消耗。因此,重要的是要注意一起使用 -wait -tail 命令的檔案型別。

Windows PowerShell 配合 Cat 實現 tail Unix 命令功能

對於更熟悉類 Unix 系統的使用者來說,使用 cat 命令會很方便。

PS C:\Users> cat .\DummyLogFile.txt

僅使用 cat 命令將輸出或列印文字檔案的內容,僅類似於 Get-Content

2021-12-31 14:49:47, Info                  CBS    TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info                  CBS    TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info                  CBS    Startup processing thread terminated normally
2021-12-31 14:49:47, Info                  CBS    TI: Startup Processing completes, release startup processing lock.
 ...

但是,使用 cat-Tail n 命令(n 是輸出中所需的最後行數)將提供與 Unix 中的 tail 命令類似的輸出。

PS C:\Users> cat .\DummyLogFile.txt -Tail 4

程式碼的輸出如下。

C:\Users>  cat .\DummyLogFile.txt -Tail 4
2022-01-06 08:58:10, Info                  CBS    Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info                  CBS    Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info                  CBS    Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info                  CBS    Ending TrustedInstaller finalization.
PS C:\Users>

需要時,cat 命令也可以與 Wait 命令一起使用,以跟蹤對文字檔案的實時更改,例如系統中的日誌檔案。

PS C:\Users> cat .\DummyLogFile.txt -Tail 4 -Wait

因此,將給出輸出,並且系統等待給定檔案中的任何更改。然後這些更改的檔案尾行將由 PowerShell 列印為輸出。

Get-Content .\DummyLogFile.txt -Wait -Tail 4
2022-01-06 08:58:10, Info                  CBS    Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info                  CBS    Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info                  CBS    Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info                  CBS    Ending TrustedInstaller finalization.
_

Linux tail 最常用於跟蹤 Linux 和類 Linux 系統中的不同日誌檔案。因此,使 Windows 使用者能夠執行命令列指令碼來實現某些類似功能的 PowerShell 需要具備這樣的功能。

在較新的 PowerShell 版本中,tail 命令與上述 Get-Contentcat 命令一起使用,wait 命令用於跟蹤任何實時更改。

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.