How to get the captured groups from Select-String?

前端 未结 5 804
清酒与你
清酒与你 2020-12-05 16:41

I\'m trying to extract text from a set of files on Windows using the Powershell (version 4):

PS > Select-String -AllMatches -Pattern 

        
5条回答
  •  佛祖请我去吃肉
    2020-12-05 17:38

    This worked for my situation.

    Using the file: test.txt

    // autogenerated by script
    char VERSION[21] = "ABCDEFGHIJKLMNOPQRST";
    char NUMBER[16] = "123456789012345";
    

    Get the NUMBER and VERSION from the file.

    PS C:\> Select-String -Path test.txt -Pattern 'VERSION\[\d+\]\s=\s\"(.*)\"' | %{$_.Matches.Groups[
    1].value}
    
    ABCDEFGHIJKLMNOPQRST
    
    PS C:\> Select-String -Path test.txt -Pattern 'NUMBER\[\d+\]\s=\s\"(.*)\"' | %{$_.Matches.Groups[1
    ].value}
    
    123456789012345
    
    

提交回复
热议问题