Mirco Lueneburg
Mirco Lueneburg

Reputation: 13

Formatting text in PowerShell

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?!?!

Upvotes: 1

Views: 702

Answers (2)

SpellingD
SpellingD

Reputation: 2621

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
}

Upvotes: 0

CB.
CB.

Reputation: 60910

try:

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

Upvotes: 4

Related Questions