1

need some help with building a powershell script to help with some basic string manipulation. I know just enough powershell to get in trouble, but can't figure out the syntax or coding to make this work.

I have a text file that looks like this -

Here is your list of servers:
server1
server2.domain.local
server3
Total number of servers: 3

I need to take that text file and drop the first and last lines (Always first and last.) Then I need to take every other line and basically turn it into a CSV file.

The final output should be a text file that looks like this -

server1,server2.domain.local,server3

Any suggestions on where to start? Thanks!

1
  • Wish I could accept all three - all three were helpful and each worked in their own way. Thanks all! Commented Jul 10, 2012 at 18:28

3 Answers 3

1

You can try :

$a = Get-Content C:\temp\M.TXT # Where M.TXT is your original file
$a[1..($a.count-2)] | % {$s=""} {$s+=$_+","}{$s.trim(",") | Out-File m.csv} # M.CSV is the result file
Sign up to request clarification or add additional context in comments.

4 Comments

Close - works perfect, other than we still have the last line "Total number of servers: 3" in the output. Any ideas?
@JPBlanc, I suspect his file as an additional blank line at the end of the file.
Looks like that might be it. I just changed the count-2 to count-3. This worked. Thanks!
@KeithHill you are right, I just not think about that at the moment.
0

Here's a couple of other ways.

(Get-Content foo.txt | Where {$_ -notmatch '^\s*$'} | Select -Skip 1 | 
     Select -Skip 1 -Last 2E9) -join ',' > foo.csv

And if you happen to have the PowerShell Community Extensions (for Skip-Object):

(Get-Content foo.txt | Where {$_ -notmatch '^\s*$'} | 
     Skip-Object -First 1 -Last 1) -join ',' > foo.csv

Note that the Where {$_ -notmatch '^\s*$'} eliminates any blank lines.

3 Comments

Close - the first method works, other than we still have the last line "Total number of servers: 3" in the output. The second select -skip statment, which has the last 2e9 (What does that mean, anyway?) - are you trying to use that to skip the last line?
@Pat I believe the -Skip/-Last parameter combo might be new in PowerShell v3. You can skip from the end of the file contents if you use -Skip in conjunction with -Last but in this case you want all of the lines. That's why I specify a large number 2E9 for the Last parameter - to get all the lines in the file except for the 1 skipped line at the end.
On the second skip 1 statement - I changed it to skip 2 and it worked like a charm. Thanks!
0

You can use the range operator:

$content = Get-Content D:\Scripts\Temp\test1.txt
$content[1..($content.count-2)] -join ',' | Out-File servers.txt

It would be better to export to a csv file so you can import it back and operate on the server names:

$content = Get-Content D:\Scripts\Temp\test1.txt
$content[1..($content.count-2)] | Select-Object @{Name='ComputerName';e={$_}} | Export-Csv servers.csv -NoTypeInformation

2 Comments

This approach won't work if there are any blank lines at the end of the file (or at the beginning or anywhere in between).
I imagine the count-3 would work here as well if there's a blank line. Thanks!

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.