locked
How to code it using Powershell Script? RRS feed

  • General discussion

  • Referring to following coding, I would like to know on how to use loop to repeat searching matched word until the end of file.

    $Source = Get-Content 'C:\Source.txt'
    $name = [regex]::match($Source, '"name":"(.*?)"').Groups[1].Value
    Write-Host "$name"

    Does anyone have any suggestions?
    Thanks in advance for any suggestions

    C:\Source.txt contains following text

    "name":"ABC" Go to School by Bus

    "name":"XYZ" Go to Library for reading

    It should display following text

    ABC

    XYZ


    Thanks in advance for any suggestions


    Sunday, December 31, 2017 3:10 AM

All replies

  • Run this command:

    help Select-String -full

    Read how to use that cmdlet. It can find strings in a file that match a regular expression pattern.


    -- Bill Stewart [Bill_Stewart]

    Sunday, December 31, 2017 5:42 AM
  • The following cmd already find the first matched item, but I need a loop to find the next matched items until EOF.

    Do you have any suggestions?
    Thank you very much for any suggestions (^v^)

    $Source = Get-Content 'C:\Source.txt'
    $name = [regex]::match($Source, '"name":"(.*?)"').Groups[1].Value
    Write-Host "$name"


    Thanks in advance for any suggestions

    Sunday, December 31, 2017 6:00 AM
  • A good place to start would be here: Learn PowerShell  

    Get-Content <filename> |
         Foreach-Object{
              ...code ...
        }


    \_(ツ)_/

    Sunday, December 31, 2017 8:38 AM
  • I would like to ONLY display $name when string is MATCHED.

    Do you have any suggestions on how to add a condition within Foreach-Object?
    Thanks, to everyone very much for any suggestions (^v^)

    Get-Content 'C:\Source.txt' |
    Foreach-Object{
    $name = [regex]::match($Source, '"name":"(.*?)"').Groups[1].Value
    Write-Host "$name"
    }


    Thanks in advance for any suggestions

    Sunday, December 31, 2017 9:27 AM
  • help about_if

    Please start with this: Learn PowerShell  


    \_(ツ)_/

    Sunday, December 31, 2017 9:31 AM
  • For Foreach-Object, I would like to know on how to determine on whether Object match "name":" or not within if statement. Any example?

    Furthermore, based on following statement, it directly find the matched pattern immediately. Comparing Foreach-Object, [regex]::match approach is much much faster. 

    $name = [regex]::match($Source, '"name":"(.*?)"').Groups[1].Value

    After find the first matched one, would it be possible to keep looking for the next one instead of using Foreach-Object?

    Does anyone have any suggestions?
    Thanks in advance for any suggestions



    Thanks in advance for any suggestions



    Sunday, December 31, 2017 9:45 AM
  • help about_Regular_Expressions

    https://www.regular-expressions.info/

    A loop contuse to match line by line and finds all matches.

    Please take the time to learn PowerShell as we cannot teach you this one command at a time.  Once you understand basic PowerShell and have learned basic programming elements all of this will become clear.

    Get-Content C:\Source.txt | Where {$_ -match '"name":"(.*)"'} | Foreach{$matches[1]}


    \_(ツ)_/


    • Edited by jrv Sunday, December 31, 2017 9:58 AM
    Sunday, December 31, 2017 9:56 AM
  • It works if C:\Source.txt contains like following texts

    "name":"ABC" Go to School by Bus, display ABC

    "name":"XYZ" Go to Library for reading, display XYZ

    -------------------------------------------------------------

    but it fails to retrieve XYZ if C:\Source.txt contains like following texts

    "name":"ABC" Go to School by Bus"name":"XYZ" Go to Library for reading, only display ABC and missing XYZ

    Do you have any suggestions on how to handle this condition?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, December 31, 2017 10:14 AM
  • In that case add a + to the capture group.  Warning.  In Windows 10 1709 the greedy capture does not work.


    \_(ツ)_/

    Sunday, December 31, 2017 10:24 AM
  • I know the capture group look like following statement, could you please show me on how to add capture group into following coding?

    #[regex]::match(Get-Content 'C:\Source.txt', '"name":"(.*?)"').Groups[1].Value ' Capture Group

    Get-Content C:\Source.txt | Where {$_ -match '"name":"(.*)"'} | Foreach{$matches[1]}

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions




    Sunday, December 31, 2017 10:29 AM
  • You have a capture group defined in your RegEx.  Just add a + to it. ()+

    Please take the time to learn both PowerShell and basic RegEx. You have been doing this for years and still won't take the time to learn the technology.

    https://www.regular-expressions.info/captureall.html

    PowerShell, RegEx and programming are difficult technical problems when you do not understand the basics.  Save yourself from ongoing frustration and do the tutorials.


    \_(ツ)_/

    Sunday, December 31, 2017 10:40 AM
  • Based on following, there are different coding with different result

    Case 1: Get-Content C:\Source.txt | Where {$_ -match '"name":"(.*?)"'} | Foreach{$matches[1]}, which show ONLY ABC

    Case 2 : Get-Content C:\Source.txt | Where {[regex]::match($_, '"name":"(.)+"').Groups[1].Value} | Foreach{$matches[1]}, which show only Z

    C:\Source.txt contains following text

    "name":"ABC" Go to School by Bus"name":"XYZ" Go to Library for reading

    It should display following text

    ABC

    XYZ

    Do you have any suggestions on how to solve this issue?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions



    Sunday, December 31, 2017 10:51 AM
  • Get-Content D:\Documents\TS\txt\Cookies.txt | Where-Object { $_ -match '"name":"(.*)+"' } | ForEach-Object{ $matches[1] }
    


    \_(ツ)_/

    Sunday, December 31, 2017 11:03 AM
  • It seems taking long period and show nothing as result

    Get-Content C:\Source.txt | Where-Object { $_ -match '"name":"(.*)+"' } | ForEach-Object{ $matches[1] }, which return nothing as result

    Get-Content C:\Source.txt | Where-Object { $_ -match '"name":"(.*)"' } | ForEach-Object{ $matches[1] }, which return ABC" Go to School by Bus"name":"XYZ as result

    It should display following text

    ABC

    XYZ

    Do you have any suggestions on what wrong it is?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions





    Sunday, December 31, 2017 11:15 AM
  • As I noted, there is a bug in the system.  I think the bug is in the Net classes.  I have no  solution for this an I have reported it to UserVoice: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/32778964-regex-pattern-match-causes-infinite-loop


    \_(ツ)_/

    Sunday, December 31, 2017 11:24 AM
  • Is there any other approach to do that?

    It should display following text

    ABC

    XYZ

    Do you have any other suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, December 31, 2017 11:36 AM
  • Get-Content C:\Source.txt | Where-Object { $_ -match '"name":"(.*)+"' } | ForEach-Object{ $matches[1] }


    \_(ツ)_/

    Could you please edit your post back to directory from original post question?

    Thanks you very much


    Thanks in advance for any suggestions



    Why?  It is my example.  Edit it as you like,.

    \_(ツ)_/

    Sunday, December 31, 2017 11:43 AM
  • No.  You will have to write a script or use FlashExtract:

    https://blogs.msdn.microsoft.com/powershell/2014/10/31/convertfrom-string-example-based-text-parsing/

    Flash Extract Video

    http://vuminhle.com/pdf/pldi14-flashextract.pdf


    \_(ツ)_/

    I am working on PowerShell script now, but there is bug, should I wait for the bug being fixed?

    The second option is FlashExtract, I would like to know on whether FlashExtract is powershell language or not.

    Do you have any other suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions

    Sunday, December 31, 2017 12:02 PM
  • The second option is FlashExtract, I would like to know on whether FlashExtract is powershell language or not.

    If you actually read the links you would know. Please put some of your own effort into this. We cannot do your thinking for you.


    \_(ツ)_/

    Sunday, December 31, 2017 12:06 PM