1

I´m stuck on a problem... I´m trying to format text in powershell.

Here´s what I´m trying to do:

I´ve got a plain-text file containing the employee id and mail address from an ldap export which I´ve already cleaned up and looks like this:

0001  
[email protected]  
0002  
[email protected]  
0003  
[email protected]  
....  
0400
[email protected]  

And I want convert this to the following:

0001,[email protected]  
0002,[email protected]  
0003,[email protected]  
...  
0400,[email protected] 

The only thing which I´ve found so far is that I could create an output which will look like this:

0001,[email protected],0002,[email protected],0003,[email protected],[email protected]

Can someone help me with this?!?!

2 Answers 2

4

try:

 gc .\listfile.txt -ReadCount 2  | % { $_ -join ',' } | out-file .\newlist.txt
0
0

First you will want to store the contents of the file:

$c=gc Myfile.txt

Then you will want to use a loop to print out every i-th and i+1-th line to a file:

for($i=0;$i -lt $c.Count;$i+=2){
    "$($c[$i]),$($c[$i+1])" | Out-File -Append MyNewFile.txt
}
2
  • I´m getting errors Cannot index into a null array. At C:\convert1.ps1:3 char:4 + $t[ <<<< $i+1] + CategoryInfo : InvalidOperation: (147:Int32) [], RuntimeException + FullyQualifiedErrorId : NullArray But Christian´s posts has already helped me ;) Commented Nov 27, 2012 at 16:10
  • Oops, I forgot to change my letters! $t --> $c
    – SpellingD
    Commented Nov 27, 2012 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.