Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first n characters of a large file with PowerShell

Tags:

powershell

I've got a huge XML file (0.5 GB), with no line breaks. I want to be able to look at, say, the first 200 characters without opening the whole file. Is there a way to do this with PowerShell?

like image 516
Jonny Cundall Avatar asked Sep 21 '13 17:09

Jonny Cundall


People also ask

Can you grep in PowerShell?

When you need to search through a string or log files in Linux we can use the grep command. For PowerShell, we can use the grep equivalent Select-String . We can get pretty much the same results with this powerful cmdlet. Select-String uses just like grep regular expression to find text patterns in files and strings.

How do you use N in PowerShell?

New line (`n)The new line ( `n ) character inserts a line break immediately after the character. This example shows how to use the new line character to create line breaks in a Write-Host command.

How do I get the first letter of a string in PowerShell?

PowerShell Get First Character of StringUse the Substring method over the given string and pass the start index as 0 to the start point of the character and length as 1 to get a number of characters to return. The output of the above script gets the first character of a string.

How do I get the content of a file in PowerShell?

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.


2 Answers

PowerShell Desktop (up to 5.1)

You can read at the byte level with Get-Content like so:

$bytes = Get-Content .\files.txt -Encoding byte -TotalCount 200
[System.Text.Encoding]::Unicode.GetString($bytes)

If the log file is ASCII you can simplify this to:

[char[]](Get-Content .\files.txt -Encoding byte -TotalCount 200)

PowerShell Core 6.0 and newer

PowerShell Core doesn't support byte encoding. It's been replaced by -AsByteStream parameter.

$bytes = Get-Content .\file.txt -AsByteStream -TotalCount 200
[System.Text.Encoding]::Unicode.GetString($bytes)
like image 100
Keith Hill Avatar answered Sep 30 '22 13:09

Keith Hill


Copying binary files via powershell commandlets tend to be a bit slow. You may, however, run the following commands from powershell to get a decent performance:

cmd /c copy /b "large file.ext" "first n.ext"
FSUTIL file seteof "first n.ext" $nbytes

Tested in Win 10 PS 5.1
Result: 1.43GB processed in 4 seconds

like image 42
Zimba Avatar answered Sep 30 '22 13:09

Zimba