Extract substring and make it a variable

Welcome Forums General PowerShell Q&A Extract substring and make it a variable

This topic contains 5 replies, has 5 voices, and was last updated by

 
Participant
1 year, 6 months ago.

  • Author
    Posts
  • #70417

    Participant
    Points: 0
    Rank: Member

    I'm all alone in a group where I'm becoming a one man devops crew and working on the start of some logging/monitoring scripts that I need some feedback on, so I'll get right to it.

    I am doing log analysis and need to extract some information out of each log file. In this world there are 200 nodes. Each node has several subdirectories (Days), and each day has multiple log files depending on how many times they booted.

    \Node001\day001\log001\Logfile.log

    I'm starting with one log file and I will scale up from that. I have a start, that works but I'm pretty sure I'm going at this the wrong way.

    I'm looking to the group to get some better suggestions on how to do this.

  • #70418

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    ...to do what?

    You can use the Substring method (https://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx) of a string.

    $sub = $mystring.substring(2,4)

    But I'm not sure that's what you're asking.

    • #70498

      Participant
      Points: 0
      Rank: Member

      First, I want to say thank you to Don for all of the powershell material you have put out. The videos and books have gotten me to this point and I really appreciate all of your time and effort. I'm sure that when I actually get time to actually read the entire lunches book, and the Dev ops manifesto

      My apologies that my post was so vauge and missing the GIST that I thought I attached, however after reading all of this, I am working on a new version.

      Here is the old stuff..

  • #70435

    Participant
    Points: 160
    Helping Hand
    Rank: Participant

    If you are looking on how to process these logs, you should look at Get-ChildItem and Get-Content to recurse the directory structure and open and parse the logs.

    #Assuming C:\Nodes is the root of the logs
    foreach ($file in (Get-ChildItem -Path C:\Nodes -Include *.log -File -Recurse)) {
        "Processing log file {0}" -f $file
        foreach ($line in Get-Content $file.FullName) {
            #Do something in each log
        }
    }
    
  • #70436

    Participant
    Points: 0
    Rank: Member

    cool

  • #70439

    Participant
    Points: 51
    Rank: Member

    Another option is using findstr and filter each log for a select text.

    https://technet.microsoft.com/en-us/library/bb490907.aspx

The topic ‘Extract substring and make it a variable’ is closed to new replies.