How to print a certain line of a file with PowerShell?

后端 未结 6 1139
野趣味
野趣味 2020-12-13 02:49

I don\'t have a decent text editor on this server, but I need to see what\'s causing an error on line 10 of a certain file. I do have PowerShell though...

6条回答
  •  悲哀的现实
    2020-12-13 03:11

    You can use the -TotalCount parameter of the Get-Content cmdlet to read the first n lines, then use Select-Object to return only the nth line:

    Get-Content file.txt -TotalCount 9 | Select-Object -Last 1;
    

    Per the comment from @C.B. this should improve performance by only reading up to and including the nth line, rather than the entire file. Note that you can use the aliases -First or -Head in place of -TotalCount.

提交回复
热议问题