read a text file and extract data and display data in columns to a .csv format using powershell/vbscript

TB 21 Reputation points
2021-04-14T17:16:42.553+00:00

Hi,

I have a text file with data, say a list of computer's names and its description, memory, cpu, os...etc. Right now it's in a text file with a single long column. I want to be able to extract by computer's name and display them into a new file (if possible) by each computer's name as the headers

For ex:

PC1 PC2 PC3
win10 win7 win8
cpu1 cpu2 cpu3

Any help will gladly be appreciated.

Windows for business Windows Server User experience PowerShell
{count} votes

14 answers

Sort by: Most helpful
  1. TB 21 Reputation points
    2021-04-14T17:27:36.61+00:00

    Sorry, I forgot to mention that instead of PC1, PC2 and PC3, the header will be reading in as Node Names.

    So, it will be something like this:

    Node Name: PC1 Node Name: PC2 Node Name: PC3
    Win10 Win7 Win8
    cpu1 cpu2 cpu3


  2. TB 21 Reputation points
    2021-04-14T19:21:55.57+00:00

    Yes, that's the input from a text file.
    Node Name: PC1
    Win10
    cpu1
    Node Name: PC2
    Win7
    cpu2
    Node Name: PC3
    Win8
    cpu3

    So far, I only have very little...it's only reading from a text file.
    Something like this: get-content c:\test\Debug\Node.log | select-string "Node Name/"


  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-04-14T20:25:41.44+00:00

    Hi @TB ,

    maybe this helps to get started:

    $inputfile = "junk/junk.txt"  
    $outputfile = "junk/junk.csv"  
    $a = Get-Content -Path $inputfile  
    $output = ""  
    $f=for ($i=0;$i -lt $a.count;$i+=3) {$a[$i]}  
    $output = ($f.Replace("Node Name: ","") -join",") + "`n"   
    $s=for ($i=1;$i -lt $a.count;$i+=3) {$a[$i]}  
    $output += ($s -join",") + "`n"   
    $t=for ($i=2;$i -lt $a.count;$i+=3) {$a[$i]}  
    $output += $t -join","  
    $output | Out-File -Filepath $outputfile  
    

    Inputfile looks like this:

    Node Name: PC1  
    Win10  
    cpu1  
    Node Name: PC2  
    Win7  
    cpu2  
    Node Name: PC3  
    Win8  
    cpu3  
    

    Outputfile looks like this:

    PC1,PC2,PC3  
    Win10,Win7,Win8  
    cpu1,cpu2,cpu3  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. TB 21 Reputation points
    2021-04-14T20:35:15.067+00:00

    Are these lines: $f=for ($i=0;$i -lt $a.count;$i+=3
    and $s=for ($i=1;$i -lt $a.count;$i+=3, only go up to 3? Can it be in a loop until there's no more Node Name left?


  5. TB 21 Reputation points
    2021-04-14T21:02:21.303+00:00

    I try your code and the output is showing one long row...

    The results that I'd like to achieve is below with Node Name and the name of the PCs as my headers.:

    Node Name: PC1 Node Name: PC2 Node Name: PC3
    Win10 Win7 Win8
    cpu1 cpu2 cpu3
    ........etc...there are more Node Names and other data...but this is just a sample data

    As for my text file, it contains one long single column like this:
    Node Name: PC1
    Win10
    cpu1
    Node Name: PC2
    Win7
    cpu2
    Node Name: PC3
    Win8
    cpu3

    Hope that makes sense and helps.

    BTW, thanks AndreasBaumgarten for your quick response :)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.