Insert delimiters

Hi,

I tried to insert Pipe-Delimiters at positions in text file, but i get this error, please some can help sort it.

$path ="c:\bkps\sample.txt"
$Positions=@(2,9,16,32) 
$dat = Get-Content $path
$Positions | foreach {$dat 
$dat=$dat.Insert($_,'|') 
}
$dat > $path


Method invocation failed because [System.Object[]] doesn't contain a method named 'Insert'.
At line:5 char:17
+ $dat=$dat.Insert <<<< ($_,'|') 
    + CategoryInfo          : InvalidOperation: (Insert:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

 
October 23rd, 2014 6:10am

$path ="c:\sample.txt"
Add-Content $Path "12345678901234567890123456789012345678901234567890"

$NewPath="C:\NewSample.txt" 

$Positions=@(2,9,16,32) 
$dat = Get-Content $path
ForEach ($Line in $Dat)
{
    For($I=$Positions.Count-1;$I -GE 0;$I--)
    {
        $Line=$Line.Insert($Positions[$I],"|")
    }
    Add-Content $NewPath $Line
}
Get-Content $NewPath

Gives this output

12|3456789|0123456|7890123456789012|345678901234567890

Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2014 6:36am

Brian's code has a very important subtlety; note that it uses the positions in descending order.  That way you haven't moved the target for the earlier positions as you modify the string. If you were to take his code and do the inner For loop in the "natural" ascending order, you'd wind up with this output instead:

12|345678|901234|567890123456789|012345678901234567890

The first pipe is in the right spot, then the second one is off by one, the third is off by two, and so on.

October 23rd, 2014 8:25am

Brian's code has a very important subtlety; note that it uses the positions in descending order.  That way you haven't moved the target for the earlier positions as you modify the string. If you were to take his code and do the inner For loop in the "natural" ascending order, you'd wind up with this output instead:

12|345678|901234|567890123456789|012345678901234567890

The first pipe is in the right spot, then the second one is off by one, the third is off by two, and so on.

Another way to approach that is to reverse the positions array and then do a normal iteration:

$string = "12345678901234567890123456789012345678901234567890"
$Positions=@(2,9,16,32) 

[array]::Reverse($Positions)
$Positions | foreach {$string = $string.Insert($_,'|')}

$string

12|3456789|0123456|7890123456789012|345678901234567890

Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2014 8:57am

Either way, it's in descending order.  The safest thing might be to pipe the array through Sort-Object, if you're not sure that it's already sorted.
October 23rd, 2014 9:25am

$path ="c:\sample.txt"
Add-Content $Path "12345678901234567890123456789012345678901234567890"

$NewPath="C:\NewSample.txt" 

$Positions=@(2,9,16,32) 
$dat = Get-Content $path
ForEach ($Line in $Dat)
{
    For($I=$Positions.Count-1;$I -GE 0;$I--)
    {
        $Line=$Line.Insert($Positions[$I],"|")
    }
    Add-Content $NewPath $Line
}
Get-Content $NewPath

Gives this output

12|3456789|0123456|7890123456789012|345678901234567890

Free Windows Admin Tool Kit Click here and download it now
October 23rd, 2014 1:22pm

The answer is simple (but not for the rubbishellians above):

[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})


Anyone can test the code above with:

      $line='12345678901234567890123456789012345678901234567890'
      $indexes=@(2,9,16,32)

and the output is:

      12|3456789|0123456|7890123456789012|345678901234567890

Simple!

      ( ... Sort-Object? ridiculous ... )


October 24th, 2014 6:20pm

Either way, it's in descending order.  The safest thing might be to pipe the array through Sort-Object, if you're not sure that it's already sorted.

Sort-Object?...

You are ridiculous, a real rubbishell coder (#1).

Free Windows Admin Tool Kit Click here and download it now
October 24th, 2014 6:23pm

The answer is simple (but not for the rubbishellians above):

[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})


Anyone can test the code above with:

      $line='12345678901234567890123456789012345678901234567890'
      $indexes=@(2,9,16,32)

and the output is:

      12|3456789|0123456|7890123456789012|345678901234567890

Simple!

      ( ... Sort-Object? ridiculous ... )


October 24th, 2014 6:27pm

I find it fairly amusing that you say it's ridiculous to sort the array of indexes, then post code that breaks if the array isn't sorted. Go ahead: change the order of anything in that array and see what happens.  Then, go get a life.
Free Windows Admin Tool Kit Click here and download it now
October 24th, 2014 9:52pm

The answer is simple (but not for the rubbishellians above):

[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})


Anyone can test the code above with:

      $line='12345678901234567890123456789012345678901234567890'
      $indexes=@(2,9,16,32)

and the output is:

      12|3456789|0123456|7890123456789012|345678901234567890

Simple!

      ( ... Sort-Object? ridiculous ... )


  • Edited by __ritehere Friday, October 24, 2014 10:25 PM
October 25th, 2014 1:15am

The answer is simple (but not for the rubbishellians above):

[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})


Anyone can test the code above with:

      $line='12345678901234567890123456789012345678901234567890'
      $indexes=@(2,9,16,32)

and the output is:

      12|3456789|0123456|7890123456789012|345678901234567890

Simple!

      ( ... Sort-Object? ridiculous ... )


  • Edited by __ritehere Friday, October 24, 2014 10:25 PM
Free Windows Admin Tool Kit Click here and download it now
October 25th, 2014 1:15am

The answer is simple (but not for the rubbishellians above):

[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})


Anyone can test the code above with:

      $line='12345678901234567890123456789012345678901234567890'
      $indexes=@(2,9,16,32)

and the output is:

      12|3456789|0123456|7890123456789012|345678901234567890

Simple!

      ( ... Sort-Object? ridiculous ... )


  • Edited by __ritehere Friday, October 24, 2014 10:25 PM
October 25th, 2014 1:15am

I find it fairly amusing that you say it's ridiculous to sort the array of indexes, then post code that breaks if the array isn't sorted. Go ahead: change the order of anything in that array and see what happens.  Then, go get a life.

You are ridiculous!

>> Go ahead: change the order of anything in that array and see what happens. 

Simple:

          $line='JIHGFEDCBAABCDEFGHIJ98765432100123456789ZAYBXCWDVE'

Then, the full code will be:

$line='JIHGFEDCBAABCDEFGHIJ98765432100123456789ZAYBXCWDVE'
$indexes=@(2,9,16,32)


[string]($indexes | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})



# the output is:
# JI|HGFEDCB|AABCDEF|GHIJ987654321001|23456789ZAYBXCWDVE


Do you see any problem, rubbishell coder #1?

Placing both outputs one after the other:

# JI|HGFEDCB|AABCDEF|GHIJ987654321001|23456789ZAYBXCWDVE
# 12|3456789|0123456|7890123456789012|345678901234567890

We can see, clearly, the pipe chars are placed at the correct position, independently of any previous sorting.

Conclusion: Sort-Object proposal is ridiculous!

Go get a new brain and, this time, don't replace it by ashes of weed.

Free Windows Admin Tool Kit Click here and download it now
October 25th, 2014 6:34am

I said the array, genius, not the string. If you really need your hand held that much, then here you go, try this:

$line='12345678901234567890123456789012345678901234567890'
$indexes=@(9,2,32,16)

October 25th, 2014 9:42am

I said the array, genius, not the string. If you really need your hand held that much, then here you go, try this:

$line='12345678901234567890123456789012345678901234567890'
$indexes=@(9,2,32,16)

Free Windows Admin Tool Kit Click here and download it now
October 25th, 2014 4:31pm

I said the array, genius, not the string. If you really need your hand held that much, then here you go, try this:

$line='12345678901234567890123456789012345678901234567890'
$indexes=@(9,2,32,16)

You are ridiculous, rubbishell coder # 1.

Are you so slow, can't you see the original post is different from yours?

If someone changes the code in any original post, the answers will fail.

get a life, retarded SHole.

If this issue were a problem (but it isn't, see the OP), just include the sort cmdlet:

[string]($indexes | sort | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})

This is no problem for anyone who knows to code, rubbishell coder # 1...

   ... but it is for you.

You are ridiculous, SHole.

October 29th, 2014 9:45pm

I said the array, genius, not the string. If you really need your hand held that much, then here you go, try this:

$line='12345678901234567890123456789012345678901234567890'
$indexes=@(9,2,32,16)

You are ridiculous, rubbishell coder # 1.

Are you so slow, can't you see the original post is different from yours?

If someone changes the code in any original post, the answers will fail.

get a life, retarded SHole.

If this issue were a problem (but it isn't, see the OP), just include the sort cmdlet:

[string]($indexes | sort | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})

This is no problem for anyone who knows to code, rubbishell coder # 1...

   ... but it is for you.

You are ridiculous, SHole.

  • Edited by __ritehere Thursday, October 30, 2014 1:39 AM
Free Windows Admin Tool Kit Click here and download it now
October 30th, 2014 4:38am

I said the array, genius, not the string. If you really need your hand held that much, then here you go, try this:

$line='12345678901234567890123456789012345678901234567890'
$indexes=@(9,2,32,16)

You are ridiculous, rubbishell coder # 1.

Are you so slow, can't you see the original post is different from yours?

If someone changes the code in any original post, the answers will fail.

get a life, retarded SHole.

If this issue were a problem (but it isn't, see the OP), just include the sort cmdlet:

[string]($indexes | sort | %{
    $ofs,$i='|',0
}{
    $line.Substring($i,($_-$i))
    $i=$_
}{
    $line.Substring($i)
})

This is no problem for anyone who knows to code, rubbishell coder # 1...

   ... but it is for you.

You are ridiculous, SHole.

  • Edited by __ritehere Thursday, October 30, 2014 1:39 AM
October 30th, 2014 4:38am

You are such a hypocrite.  Right from the moment you joined this thread, it was to say "Sort-Object? RIDICULOUS!" as though you have any idea what you're talking about, other than to be a troll. Then in your most recent post, you say "If this issue were a problem (but it isn't, see the OP), just include the sort cmdlet."

Gee, thanks for agreeing with the original point I made, which you came in here solely to bitch about.

Free Windows Admin Tool Kit Click here and download it now
October 30th, 2014 8:02am

You are such a hypocrite.  Right from the moment you joined this thread, it was to say "Sort-Object? RIDICULOUS!" as though you have any idea what you're talking about, other than to be a troll. Then in your most recent post, you say "If this issue were a problem (but it isn't, see the OP), just include the sort cmdlet."

Gee, thanks for agreeing with the original point I made, which you came in here solely to bitch about.

You are pretty much RIDICULOUS, rubbishell coder # 1.

Are you able to understand that the indexes are hard-coded?

Are you able to understand that the hard-coded values are set in incresing order?

If they were not, anyone could easily 'fix' it:

          $indexes = ... # some assorted order
          $indexes = $indexes | sort

Are you able to understand it, SHole?

Are you so stupid to not understand it?

Why did you consider the possibility of indexes in any different order than the one the OP has hard-coded them?

Why haven't you considered too, the indexes being not unique, besides being out of order?

The answer is simple, mvp ignorant: you are not able to chain more than 2 thoughts, without forgetting the first one, because you replaced any low quality brain you had by ashes of weed, stupid.

Sort-Object? You are ridiculous (and slow, and ignorant, and a rubbishell coder, ... and such a huge SHole).

... and you need a very low valued mcc to mark your stupid reply as correct! The same one that marked as correct the stupid post in this thread:

http://social.technet.microsoft.com/Forums/en-US/318b2885-c00e-4daf-8335-8a4beff5a52d/how-to-delete-contents-of-a-csv-file-except-header-using-powershell?forum=winserverpowershell

The same thread that you showed yourself as a coward and stupid huge SHole, along with the pretty much nothing and stupid mike laughable, who makes laugh whenever *it* posts some code...

Sort-Object? You are ridiculous (and slow, and ignorant, and a rubbishell coder, ... and such a huge SHole). Why did you forget uniqueness? ... SHole.

November 4th, 2014 7:37am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics