none
Reading a text file and extracting information. RRS feed

  • Question

  • I have a text file that contains machine names IP address and other information. I need a powershell script to read the text file and extract only the machine names. My code

    $File1 = Get-content "C:\*\Logins.txt"
    $file1 | Where-Object {$_.entrytype -eq"Computer*"}

    The machine names contain a common name followed by a unique numerical identifier. (example) Computer125214

     
    Tuesday, October 29, 2013 5:20 PM

Answers

  • Yep, looks like it's just a semicolon-delimited file.  I'd probably use Import-Csv, though, just because it handles quoting and embedded delimiters.  Assuming the file does not contain a header row, something like this:

    $headers = @(
        'DateTime'
        'ComputerName',
        'SomeString',  # No idea what to call that third redacted field.  UserName, maybe?
        'IPAddresses'
    )
    
    Import-Csv -Path .\Logins.txt -Delimiter ';' -Header $headers |
    Select-Object -ExpandProperty ComputerName



    Edit:

    I had overlooked this statement, which was embedded in a quote block:  "The following is an example of 3 lines with in the .txt doc all of the lines are not the same, some do not contain Mac Addresses or IP address."

    When a line doesn't contain a MAC address or IP address, does the file still contain the semicolon delimiters around those empty fields? For example:

    3/1/2013 8:18:59 AM;Computer2278;;

    as opposed to:

    3/1/2013 8:18:59 AM;Computer2278

    Depending on how the file is structured, Import-Csv might not work quite as I had intended (depending on whether the missing fields are found in the middle of the line, or only at the end.)

    Wednesday, October 30, 2013 3:15 PM
  • I have a text file that contains machine names IP address and other information. I need a powershell script to read the text file and extract only the machine names. My code

    $File1 = Get-content "C:\*\Logins.txt"
    $file1 | Where-Object {$_.entrytype -eq"Computer*"}

    The machine names contain a common name followed by a unique numerical identifier. (example) Computer125214

    More Information As requested:

    The following is an example of 3 lines with in the .txt doc all of the lines are not the same, some do not contain Mac Addresses or IP address

    3/1/2013 8:17:00 AM;Computer22761;JP*****;192.168.**.***,fe80::e5a8:d934:84e1:7eab
    3/1/2013 8:17:07 AM;Computer22763;CWERT***;198.***.***.***
    3/1/2013 8:18:59 AM;Computer22787;MON****;10.24.**.**  



    Try this:

    $lines = Get-Content C:\...
    $lines | % { $null = $_ -match '^.+(Computer\d{1,}).+$' ; $Matches.1 } 
    


    ... don't disturb ... life is short, and I have to think what I'll do next ...

    Wednesday, October 30, 2013 3:47 PM

All replies

  • Can you post a couple of complete sample lines from the file?  That will help a great deal with writing parsing code.

    I can start by pointing out a flaw with your current code.  Get-Content returns an array of String objects.  You're piping those Strings to Where-Object { $_.entrytype -eq "Computer*" }, but String objects don't have a property called "EntryType".

    Tuesday, October 29, 2013 5:29 PM
  • Is the format of the text file comma delimited? If so, it would be easiest to change the extension to csv, then you can do something like:

    $csv = Import-Csv "C:\Myfile.csv"

    ForEach($row in $csv)
    {
    $row.ComputerName $row.IPAddress
    }

    You can then access each heading in the csv file like properties to the object created. If it isn't can you supply the format of the text file, as that will determine how to go about extracting the data

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Don't Retire Technet


    • Edited by clayman2 Tuesday, October 29, 2013 5:32 PM typo
    Tuesday, October 29, 2013 5:31 PM
  • Yes, you should give us some more information esp. about the structure of that file. If its a simple text file you can try to read it line by line and pipeline the lines to a tool like Select-String. Just a coarse shot in the dark:

    $mInfo = type "C:\*\Logins.txt" | Select-String -Pattern "\bComputer(\d)+\b"
    foreach ($match in $mInfo.Matches) {$match.Value}

    Kind regards,

    wizend

    Tuesday, October 29, 2013 9:35 PM
  • I have a text file that contains machine names IP address and other information. I need a powershell script to read the text file and extract only the machine names. My code

    $File1 = Get-content "C:\*\Logins.txt"
    $file1 | Where-Object {$_.entrytype -eq"Computer*"}

    The machine names contain a common name followed by a unique numerical identifier. (example) Computer125214

    More Information As requested:

    The following is an example of 3 lines with in the .txt doc all of the lines are not the same, some do not contain Mac Addresses or IP address
    3/1/2013 8:17:00 AM;Computer22761;JP*****;192.168.**.***,fe80::e5a8:d934:84e1:7eab
    3/1/2013 8:17:07 AM;Computer22763;CWERT***;198.***.***.***
    3/1/2013 8:18:59 AM;Computer22787;MON****;10.24.**.**  


    Wednesday, October 30, 2013 1:21 PM
  • Hi,

    Try this:

    $computerList = @()
    
    Get-Content .\Logins.txt | ForEach {
    
        $computerList += $_.Split(';')[1]
    
    }
    
    $computerList

    This only returns the ComputerXXXXX information.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,300+ strong and growing)

    Wednesday, October 30, 2013 1:44 PM
    Moderator
  • Yep, looks like it's just a semicolon-delimited file.  I'd probably use Import-Csv, though, just because it handles quoting and embedded delimiters.  Assuming the file does not contain a header row, something like this:

    $headers = @(
        'DateTime'
        'ComputerName',
        'SomeString',  # No idea what to call that third redacted field.  UserName, maybe?
        'IPAddresses'
    )
    
    Import-Csv -Path .\Logins.txt -Delimiter ';' -Header $headers |
    Select-Object -ExpandProperty ComputerName



    Edit:

    I had overlooked this statement, which was embedded in a quote block:  "The following is an example of 3 lines with in the .txt doc all of the lines are not the same, some do not contain Mac Addresses or IP address."

    When a line doesn't contain a MAC address or IP address, does the file still contain the semicolon delimiters around those empty fields? For example:

    3/1/2013 8:18:59 AM;Computer2278;;

    as opposed to:

    3/1/2013 8:18:59 AM;Computer2278

    Depending on how the file is structured, Import-Csv might not work quite as I had intended (depending on whether the missing fields are found in the middle of the line, or only at the end.)

    Wednesday, October 30, 2013 3:15 PM