2 Nov 2021

Get One Line or Count the Number of Lines, Words, Characters in PowerShell

收藏到CSDN网摘

PowerShell is replacing cmd.exe in the new Windows operating systems, it's more efficient and powerful than its predecessor. The only drawback might be that the commands are much longer than what we used to see in cmd.exe. Counting the lines, words, even the characters are very useful - well, sometimes at least. This task can be easily done using some GUI software such as MS Word, Notepad, or vim if you prefer. PowerShell is capable of doing these jobs as well. Here is how ~ To count the number of lines of a (text) file in Powershell:
Get-Content FILENAME | Measure-Object –Line
To count the number of words of a (text) file in Powershell:
Get-Content FILENAME | Measure-Object –Word
To count the number of characters of a (text) file in Powershell:
Get-Content FILENAME | Measure-Object –Character
These options can be stacked together if you like: To count the number of characters of a (text) file in Powershell:
Get-Content FILENAME | Measure-Object –Line –Word –Character
To get a specific line from the (text) file, the Get-Content or gc command should work. However, it's slow. According to some test results, the fastest solution would be:
([System.IO.File]::ReadAllLines(FILE_PATH))[LINE_INDEX]

No comments :

Post a Comment