Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to read a text file and split data into separate files

$
0
0

Hello,

I have a text file with the following move-clustergroup data:

Move-ClusterGroup "SCVMM VM1 Resources" -Node HOST1
Move-ClusterGroup "SCVMM VM2 Resources" -Node HOST1
Move-ClusterGroup "SCVMM VM3 Resources" -Node HOST1
Move-ClusterGroup "SCVMM VM4 Resources" -Node HOST2
Move-ClusterGroup "SCVMM VM5 Resources" -Node HOST2
Move-ClusterGroup "SCVMM VM6 Resources" -Node HOST2

I'd like to split this file into two files by HOST.  So file 1 would have:

Move-ClusterGroup "SCVMM VM1 Resources" -Node HOST1
Move-ClusterGroup "SCVMM VM2 Resources" -Node HOST1
Move-ClusterGroup "SCVMM VM3 Resources" -Node HOST1

And file 2 would have:

Move-ClusterGroup "SCVMM VM4 Resources" -Node HOST2
Move-ClusterGroup "SCVMM VM5 Resources" -Node HOST2
Move-ClusterGroup "SCVMM VM6 Resources" -Node HOST2

I have the following script that partly does what I need:

Select-String "HOST1" -path C:\apps\Powershell_Scripts\ClusterGroup_scripts\test.txt -AllMatches -simplematch

The above script will give the following results:

test.txt:1:Move-ClusterGroup "SCVMM VM1 Resources" -Node HOST1
test.txt:2:Move-ClusterGroup "SCVMM VM2 Resources" -Node HOST1
test.txt:3:Move-ClusterGroup "SCVMM VM3 Resources" -Node HOST1

Almost there..I need to show just the exact lines like were in my file and not the name of the file...therefore remove the following "test.txt:1:"

Also, what is the instruction to write this out to a new text file?

Thanks in advance for any advice you can provide.





Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>