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
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

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 ;)
Oops, I forgot to change my letters! $t --> $c

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.