none
select string in variable from variable.

    Question

  • Hi Folks

    I'm having an issue selecting a string from an array.

    I have a File "A" with data similar to belew

    [BornPlace0] 1 3000 136669 26640 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace1] 1 3000 119248 37216 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace2] 1 3000 175195 71315 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace3] 1 3000 161795 67096 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace4] 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace5] 1 3000 175589 138093 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace6] 1 3000 125901 129376 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace7] 1 3000 121895 151749 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace8] 1 3000 87523 23310 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace9] 1 3000 32730 24390 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace10] 1 3000 44069 57505 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace11] 1 3000 91669 46262 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
     [BornPlace13] 1 3000 121244 74170 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    I then file "B" that has one of the above strings but without the [Bornplace] info

    Example:

    1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    What I'm trying to do is take File B string and match it to file A but I'd like to only get the [Bornplace] info that matches file B String. 

    so if im doing a search on 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0 id like the return info to be [BornPlace4]

    Could anyone shed any light on the command to pull that data?

    I appreciate this looks very simple but I've tried for a fiew days without any results.  :-(

    • Moved by Bill_Stewart Monday, March 24, 2014 10:16 PM Abandoned
    Saturday, September 21, 2013 12:49 PM

All replies

  • just to add File a assigned to the variable $A using the get-content command.

    File B is also set to $B variable, this might be part of the reason I'm having issues.

    Saturday, September 21, 2013 12:55 PM
  • Start by learning about basic scripting.  It would also help if you posted your script.

    Here is a starter:

    $b=cat fileb.txt
    $line=cat filea.txt | ?{$_ -match $b}
    $line -match '\[*.\]'
    $matches[0]


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, September 21, 2013 3:24 PM
    Saturday, September 21, 2013 1:55 PM
  • JRV, you should really test your code before you post.  You'd be mocking that code if someone else posted it.  In fact, word for word, your reply would probably be "It works fine for me when I removed all your coding errors.  Look really close.  See what you did wrong."

    Mike, how confident are you that the text in file "B" will exactly match what comes after [BornPlace##] in file "A" (even the number of spaces / tabs, etc)?  If it's always going to be an exact match, then the PowerShell code will be fairly simple.

    Here's a slight modification of JRV's code to fix and avoid some errors, and improve the clarity:

    # I modified this part to make sure that we're only getting a single non-whitespace String from
    # file b, not an array (if the file happens to contain multiple lines, even blank ones).
    
    $b = Get-Content fileb.txt |
         Where-Object { $_ -match '\S' } |
         Select-Object -First 1
    
    # If $b didn't have any non-blank lines, there's no point doing the rest of the code.  You could also
    # output an error message to that effect in an else block.
    
    if ($b -ne $null)
    {
        # While not strictly necessary if your file just contains numbers, it's a good idea to escape
        # any text you're planning to use in a regular expression (which is what the -match operator
        # does.)
    
        $b = [regex]::Escape($b)
    
        # Structuring this code as a loop avoids errors when no match is found.
        
        Get-Content filea.txt |
        ForEach-Object {
            # Regular Expression can be hard to read, but they're very powerful for this kind of text parsing.
            # This regex pattern looks for a string beginning with [Something] (surrounded by any amount of
            # whitespace), followed by an exact match of the line we pulled from fileb.txt (and nothing else
            # at the end of the line.)
    
            if ($_ -match "^\s*(\[.*\])\s*$b`$")
            {
                # The pattern above has a capturing group (the part surrounded by parentheses).  $matches[0]
                # will contain the entire line, and $matches[1] contains just the first capturing group; in this
                # case, just the [Something] string.
    
                $matches[1]
            }
        }
    }
    
    # And the condensed verison, without all of the comments:
    
    $b = Get-Content fileb.txt |
         Where-Object { $_ -match '\S' } |
         Select-Object -First 1
    
    if ($b -ne $null)
    {
        $b = [regex]::Escape($b)
        
        Get-Content filea.txt |
        ForEach-Object {
            if ($_ -match "^\s*(\[.*\])\s*$b`$")
            {
                $matches[1]
            }
        }
    }


    Saturday, September 21, 2013 3:09 PM
  • For all- I fixed the bad lines caused by pasting the wrong version.

    Here is the exact code which works and does not require 5 pages of nonsense to run.

    $b=cat fileb.txt
    $line=cat filea.txt | ?{$_ -match $b}
    $line -match '\[.*\]'
    $matches[0]

    This time  I retested AFTER pasting to be sure I actually pasted the correct version.


    ¯\_(ツ)_/¯

    Saturday, September 21, 2013 3:25 PM
  • Your code didn't work at all.  Here's why:

    # Original code:
    
    $b=cat-fileb.txt
    $line=cat filea.txt | ?{$_ -match $b}
    $line -match '\[(*.\)]'
    $matches[0]
    
    # There is no such command as cat-fileb.txt.  The hyphen 
    # character shouldn't be there.
    
    # There are two problems in your regular expression 
    # pattern.  The * character needs to come after the 
    # period, and the second backslash should be between the 
    # closing parenthesis and the square bracket.
    
    # If those problems are fixed, the code will work without 
    # errors, IF Get-Content returns only a single String from 
    # fileb.txt instead of an array, and IF there is a 
    # matching line in filea.txt.  If there's no match, you'll 
    # get an error for trying to index into a null $matches 
    # array.  I modified the code to handle those situations 
    # gracefully as well.

    As for "personal attacks", lately, I've just been turning the same attitude you use on others back on you.  If it makes you that mad, maybe that should tell you something about how you treat other people on these forums.  Put bluntly, you frequently act like a condescending jerk even when you're giving good information, and sometimes, you're outright insulting.  I tried pointing that out politely at first, and you ignored it, so here we are.

    Edit:  This reply doesn't make as much sense now that JRV has deleted and modified his earlier posts in the thread.

    Saturday, September 21, 2013 3:29 PM
  • Your code didn't work at all.  Here's why:

    The code was meant to show how easy it could be. The wrong version was pasted and that has been fixed.  Simply pointing out that there was a typo would have been sufficient.

    Once again - we do not provide solutions on demand.  The forum is for assistance and not consulting.


    ¯\_(ツ)_/¯


    • Edited by jrv Saturday, September 21, 2013 3:56 PM
    Saturday, September 21, 2013 3:37 PM
  • Mike -

    The point I was trying to make is that these things are very easy once you have a good handle on the basics.  This forum is a good place to learn.  I posted a sample to try and get you interested by showing how simple it can be to solve "one-off" problems without having to be a software programmer.  For Admins and other technicians PowerShell can be extremely useful.  It has more tools than you will likely ever need.  Learning  the basics of file manipulation with PowerShell is fundamental.  It can save you hours of time and you do not even need to write a script.

    The code I posted was done "ad-hoc" at the command prompt. I just quickly threw your example into a file with notepad at the command promt:

    PS >notepad filea.txt

    then paste the sample.

    With two file I just typed the code:

    PS >$b=cat fileb.txt
    PS >$line=cat filea.txt | ?{$_ -match $b}
    PS >$line -match '\[.*\]'
    PS >$matches[0]

    Of course I did do some playing around to get a better output but decided to go with the simplest version.  Unfortunately I copied and pasted half of the experimental code the first time.

    The original code comes from knowing that we want one line that matches the B file line then we want to extract the item that is between the two [] in the A file.

    If this is to be used repeatedly then you would put it in a file and add error checking and, perhaps, help.  You can do as much or as little as you need for your projects. Admin scripting can be as informal as you need.

    I do believe that it is important to cultivate good habits as you learn because this will make future problem solving more efficient.  Learning the basics cold is a good first good habit or, as we call them in the world of tinkerers, "Best Practices".

     


    ¯\_(ツ)_/¯

    • Proposed as answer by David WyattMVP Saturday, September 21, 2013 4:14 PM
    Saturday, September 21, 2013 4:10 PM
  • Hi Gents

    Sorry if I've ruffled any feathers!

    Thank you both for you replies, I really appreciate any time you have spent looking at this small issue. 

     I've been trying to learn powershell for the last year on and off.  I find it easier to read the code than actually write but I guess everyone experiences this in the early stages. 

    David, Thanks for the detail you have gone into, thats exactly the kind of info a n00b like me needs.  I'll check out both bits of code and hope u don't mind if I ask more questions if I get stuck again!

    Mike

    Saturday, September 21, 2013 10:43 PM

  • When running the code I just seem to be getting the string from file B displayed. No [Bornplace] info.

    included all my code, down the very bottom is your code.  looks different but thought it would be less complex for my first post.




    #get take off waypoint
    $content = Get-Content 'c:\fibow script\Moscow Home.mis'
    $homeb = Get-Content 'c:\fibow script\Home.mis'
    $geo = 'c:\fibow script\MO_geo_obj.data'
    $to = Get-Content 'c:\fibow script\Moscow Home.mis' | Sort-Object | get-unique | Select-String "TAKEOFF"
    $bornplace = Get-Content 'c:\fibow script\Bornplace.mis'

    #remove data from take off point leaving just coordinates
    $to = $to -replace ("TAKEOFF", "")
    #$to = $to -replace ("\.\w\w\s", " ")  
    $to = $to -replace ("0.00 0.00  &0", "")
    $to = $to -replace ("0.00 0.00 &0", "")

    #Get Waypoint and decide if Red or Blue
    $array =  $to -split (" ")
    $rob1 = get-content $geo | Select-String $array[1] |Select-String AF
    $rob1 = $rob1 -split ("")
    $rob1 = -join $rob1[1,2,3,4]
    $rob1 = Get-Content $geo | Select-String "$rob1,"
    $rob1 = $rob1 -split ("")


    $array =  $to -split (" ")
    $rob2 = get-content $geo | Select-String $array[5] |Select-String AF
    $rob2 = $rob2 -split ("")
    $rob2 = -join $rob2[1,2,3,4]
    $rob2 = Get-Content $geo | Select-String "$rob2,"
    $rob2 = $rob2 -split ("")


    $array =  $to -split (" ")
    $rob3 = get-content $geo | Select-String $array[9] |Select-String AF
    $rob3 = $rob3 -split ("")
    $rob3 = -join $rob3[1,2,3,4]
    $rob3 = Get-Content $geo | Select-String "$rob3,"
    $rob3 = $rob3 -split ("")


    $array =  $to -split (" ")
    $rob4 = get-content $geo | Select-String $array[13] |Select-String AF
    $rob4 = $rob4 -split ("")
    $rob4 = -join $rob4[1,2,3,4]
    $rob4 = Get-Content $geo | Select-String "$rob4,"
    $rob4 = $rob4 -split ("")
    #$array[1]
    $rob1=$rob1[-2]
    $rob2=$rob2[-2]
    $rob3=$rob3[-2]
    $rob4=$rob4[-2]



    #split coordinates into array
    $array =  $to -split (" ")

    #find what number starts with
    $grid1=$array[1] -Split ""
    $grid2=$array[2] -Split ""
    $grid3=$array[5] -Split ""
    $grid4=$array[6] -Split ""
    $grid5=$array[9] -Split ""
    $grid6=$array[10] -Split ""
    $grid7=$array[13] -Split ""
    $grid8=$array[14] -Split ""

    $1=$grid1[1]
    $2=-join $grid2[1,2]
    $3=$grid3[1]
    $4=-join $grid4[1,2]
    $5=$grid5[1]
    $6=-join $grid6[1,2]
    $7=$grid7[1]
    $8=-join $grid8[1,2]



    #search for first number in homebase line then next 2 numbers in second row.
    $h1 = $homeb | Select-String "$1..... $2...."
    $h1b = $homeb | Select-String "$1.... $2....."
    $h2 = $homeb | Select-String "$3..... $4...."
    $h2b = $homeb | Select-String "$3.... $4....."
    $h3 = $homeb | Select-String "$5.... $6....."
    $h3b = $homeb | Select-String "$5..... $6...."
    $h4 = $homeb | Select-String "$7.... $8....."
    $h4b = $homeb | Select-String "$7..... $8...."

    $h1 = $h1 -replace ("0 3000", "$rob1 3000")
    $h1b = $h1b -replace ("0 3000", "$rob1 3000")
    $h2 = $h2 -replace ("0 3000", "$rob2 3000")
    $h2b = $h2b -replace ("0 3000", "$rob2 3000")
    $h3 = $h3 -replace ("0 3000", "$rob3 3000")
    $h3b = $h3b -replace ("0 3000", "$rob3 3000")
    $h4 = $h4 -replace ("0 3000", "$rob4 3000")
    $h4b = $h4b -replace ("0 3000", "$rob4 3000")



    $allhb= @()
    #all 4 homebases
    $allhb = "[Bornplace]`r`n","$h1`r`n"+"$h1b`r`n"+"$h2`r`n"+"$h2b`r`n"+"$h3`r`n"+"$h3b`r`n"+"$h4`r`n"+"$h4b`r`n"

    $base1 = $allhb[1]
    $base2 = $allhb[3]
    $base3 = $allhb[5]     
    $base4 = $allhb[7]


    $base1 |
         Where-Object { $_ -match '\S' } |
         Select-Object -First 1

    if ($base1 -ne $null)
    {
        $base1 = [regex]::Escape($base1)

        $bornplace |
        ForEach-Object {
            if ($_ -match "^\s*(\[.*\])\s*$b`$")
            {
                $matches[1]
            }
        }
    }

        

    • Edited by Mike.Rae Sunday, September 22, 2013 7:29 AM error
    Saturday, September 21, 2013 11:32 PM
  • Hi Mike,

    I can't really tell what's going on with most of this code.  It depends on what's in the files that you're reading.  I can focus on just the end of the code, close to where you added some of the code I posted earlier:

    $allhb= @()
    #all 4 homebases
    $allhb = "[Bornplace]`r`n","$h1`r`n"+"$h1b`r`n"+"$h2`r`n"+"$h2b`r`n"+"$h3`r`n"+"$h3b`r`n"+"$h4`r`n"+"$h4b`r`n" 
    
    $base1 = $allhb[1]
    $base2 = $allhb[3]
    $base3 = $allhb[5]     
    $base4 = $allhb[7]
    

    It looks like you're trying to set up $allhb to be an array of strings, with one element per "line". However, PowerShell doesn't create new array elements when you embed "`r`n" in a string. What $allhb currently contains is just two elements. The first one is [Bornplace] followed by "`r`n", and the second element contains everything else. This means that none of the $base1 - $base4 variables will contain what you probably intended.  This might work better:

    $allhb = @(
        '[Bornplace]',
        $h1,
        $h1b,
        $h2,
        $h2b,
        $h3,
        $h3b,
        $h4,
        $h4b
    )

    On a side note, almost anytime I see someone naming variables "$something1", "$something2", "$something3", etc, it's an indicator that they might have been better off using an array or other type of collection. ($something[0], $something[1], foreach ($thing in $something), etc).  Using loops over the contents of a collection allows you to avoid lots of duplicated code in your script.

    Since I'm not clear on what most of this code is doing, I don't want to suggest any actual changes, but it's something you might want to keep in mind for later.

    $base1 |
        Where-Object { $_ -match '\S' } |
        Select-Object -First 1
    
    if ($base1 -ne $null)
    {
        $base1 = [regex]::Escape($base1)
    
        $bornplace |
        ForEach-Object {
            if ($_ -match "^\s*(\[.*\])\s*$b`$")
            {
                $matches[1]
            }
        }
    }

    Because of the problems with setting up the $base1 variable, this part probably wouldn't work anyway.  However, assuming that $base1 is supposed to contain a string like this: "1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0" (taken from your original post), then I'd recommend changing the code to this:

    $base1 = $allhb[1]
    $base2 = $allhb[3]
    $base3 = $allhb[5]     
    $base4 = $allhb[7]

    $escapedBase = [regex]::Escape($base1) $bornplace | ForEach-Object { if ($_ -match "^\s*(\[.*\])\s*$escapedBase`$") { $matches[1] } }

    If you've already set up the $base1 variable, and are not reading directly from a file, the first pipeline with Get-Content, Where-Object and Select-Object is unnecessary.  The other issue was that you changed some of the references from $b to $base1, except for the "$b" that was part of the regular expression pattern.  To avoid overwriting the contents of $base1 (just in case you need the original contents of $base1 later), I changed it to use a variable called $escapedBase instead.


    Sunday, September 22, 2013 1:48 AM
  • Hi David

    With the new code in place I get no results. $matches gives me the following info

    PS C:\fibow script> $Matches

    Name                           Value                                             
    ----                           -----                                             
    0                              1                                                 

    The $allhb produces the following data

    [Bornplace] <--This is correct and looks excatly how it should for this section of code.

      1 3000 109088 95478 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0



      1 3000 121244 74170 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0



      2 3000 29186 112470 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0



      2 3000 91669 46262 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    This is actually fine, the data in here is correct the [1],[3],[5],[7] produces these individually, what i'm trying to do is compare each line with the $BornPlace variable this should match the line of code on each, I then want to pull back the [BornPlace] info for each one. Would ber better if it was [1],[2],[3],[4] but thats my bad code.

    The end result will look something like this

    [Bornplace]

      1 3000 109088 95478 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
      1 3000 121244 74170 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
      2 3000 29186 112470 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
      2 3000 91669 46262 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    [Bornplace1-20]

    some data

    [Bornplace1-20]

    Some data

    [Bornplace 1-20]

    some data

    [Bornplace 1-20]

    Sunday, September 22, 2013 8:44 AM
  • It appears that you have significantly changed the original request.

    It is now very unclear what you are trying to do.

    Originally you wanted to match one line from a file with lines in a second file and return the first part of the matched line that you are calling [bornplace].  Are you saying that the format of the file has changed?  Are you trying to ask a new question?

    What is the status of your original question?  Was it answered or are you changing the question?


    ¯\_(ツ)_/¯

    Sunday, September 22, 2013 9:37 AM
  • This is the same question, My original question i tried to make a more simple example of my intentions, not including any the code knowing this would only confuse matters.

    $base1 = 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0  (first post FileB.txt)

    $bornplace = an array of strings including $base1 but with the [Bornplace4] at start (First post FileA.txt)

    $base1 | -match $bornplace | if found take start of string [Bornplace4] and place in $bornplace4

    Sunday, September 22, 2013 10:36 AM
  • Are you saying that you want to create a variable that is the same name as the string inside of the []?

    You are trying very hard to make your needs sound like a technical spec.  Can you just say in plain English what the purpose of this is?  Knowing the simple reason will make it much easier to discover a solution.


    ¯\_(ツ)_/¯

    Sunday, September 22, 2013 10:46 AM
  • If you are just trying to create variables with the name found then here is how to do that.

    $b=cat fileb.txt
    $line=cat filea.txt | ?{$_ -match $b}
    $line -match '\[.*\]'
    $name=$matches[0] -replace '[\[\]]',''
    New-Variable -Name $name -Value 0

    This finds the matching line and extracts the name within the [] at the beginning. THe name found is used to create a variable.

    From you original post the line found is tagged with [BornPlace4] The script creates a variable $BornPlace4 and assigns 0 to it.


    ¯\_(ツ)_/¯

    Sunday, September 22, 2013 10:55 AM
  • I can see my lack of experience in this field is causing frustration, not something I want to get involved in.

    Thanks for your help so far but i'll go back to the books on this one and try get a better grasp of the basics.

    Sunday, September 22, 2013 12:42 PM
  • I can see my lack of experience in this field is causing frustration, not something I want to get involved in.

    Thanks for your help so far but i'll go back to the books on this one and try get a better grasp of the basics.

    Have you tried the code I posted?

    Can you give us a plain English language version of what it is you are trying to do. So far your description is ambiguous because you are trying to use technical terms and this leaves an incomplete description.

    Just say what it is you are trying to accomplish as an outcome.  Are you trying to sort? group? count?  What is your desired outcome?


    ¯\_(ツ)_/¯

    Sunday, September 22, 2013 2:16 PM
  • Im tryin to split

    Im searching for this: 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    it finds this: [BornPlace4] 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0

    end result I just want "[Bornplace4]"

    [Bornplace4] is the part I need to capture along with some others but thats not important for the moment .  This will then be added to another file outwith the script and files i've spoken about.

    tried your code and got "New-Variable : Cannot bind argument to parameter 'Name' because it is null."

    Sunday, September 22, 2013 3:29 PM
  • Sorry - I had to re-paste this:

    Test files are:

    #FILEA.TXT
    [BornPlace0] 1 3000 136669 26640 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace1] 1 3000 119248 37216 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace2] 1 3000 175195 71315 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace3] 1 3000 161795 67096 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace4] 1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace5] 1 3000 175589 138093 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace6] 1 3000 125901 129376 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace7] 1 3000 121895 151749 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace8] 1 3000 87523 23310 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace9] 1 3000 32730 24390 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace10] 1 3000 44069 57505 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace11] 1 3000 91669 46262 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    [BornPlace13] 1 3000 121244 74170 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    
    #FILEB.TXT
    1 3000 162507 102132 1 1000 200 0 0 0 5000 50 0 0 0 0 0 3.8 0 0 0 0 0
    

    Two files in same folder and currentfolder inPowerShel CLI (do not use ISE for now)

    Copy and paste this code:

    # copy and paste into  PowerShell CLI
    $b=cat fileb.txt
    $line=cat filea.txt | ?{$_ -match $b}
    Write-Host 'Number of lines = '$line.Count -fore green
    if($line -match '\[.*\]'){
        $name=$matches[0] -replace '^[\[\]]',''
        Remove-Variable $name -ea 0
        New-Variable -Name $name -Value 0
        Get-Variable $name
        #OR
        $BornPlace4
    }else{
        Write-Host 'Matching line not found' -BackgroundColor -fore red -back white
    }
    
    

    This can be pasted multiple time or one line at a time until you understand how it works.

    Once you understand it then you can try your files.  The code will tell you about some issues with your files but will not analyze everything.


    ¯\_(ツ)_/¯

    Sunday, September 22, 2013 4:58 PM
  • Lines = 0

    Looks like its not finding a match.

    Again pls, don't spent anymore time looking at this issue, I'd like to go back to basics and see what I'm doing wrong.  Thanks for the information you provided so far. I'll look back on this once I'm more familiar.

    Monday, September 23, 2013 1:38 PM
  • Again you are not giving us any information.  The code and sample file text I posted always find a match.  If it isn't then you are doing it wrong or you are not following the instructions carefully.

    If you are failing on you own files then the lines are not a match.  This can happen.  What makes you think the line in one file is a match?


    ¯\_(ツ)_/¯

    Monday, September 23, 2013 2:32 PM