سؤال

I have a binary file that I need to process, but it contains no line breaks in it. The data is arranged, within the file, into 104 character blocks and then divided into its various fields by character count alone (no delimiting characters).

I'd like to firstly process the file, so that there is a line break (`n) every 104 characters, but after much web searching and a lot of disappointment, I've found nothing useful yet. (Unless I ditch PowerShell and use awk.)

Is there a Split option that understands character counts? Not only would it allow me to create the file with nice easy to read lines of 104 chars, but it would also allow me to then split these lines into their component fields.

Can anyone help please, without *nix options?

Cheers :)

هل كانت مفيدة؟

المحلول

$s = get-content YourFileName | Out-String
$a = $s.ToCharArray()
$a[0..103] # will return an array of first 104 chars

You can get your string back the following way, the replace removes space char( which is what array element separators turn into)

$ns = ([string]$a[0..103]).replace(" ","")

نصائح أخرى

Using the V4 Where method with Split option:

$text = 'abcdefghi'

While ($text)
{ 
  $x,$text = ([char[]]$text).where({$_},'Split',3)
  $x -join ''
}

abc
def
ghi
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top