-3

I've below sample text in a text file and wanted to get the value after id to be captured in a variable for later use.

id                          : 42cdb643-0a11-416b-b66b-7bb28a9a20ea
isReadOnly                  : False
somestring                   : False
somestring                   : {}
name                        : 

id                          : d006d69e-8d04-48d7-8806-cdc39c18e679
somestring                  : False
somestring                  : False
somestring                  : 822F0340-818A-4F23-AA78-633B6436913F
upstreamDataflowsOfDatasets : {}

I have the same set of data in the text file. I'm looking for a PowerShell code which will scan the text and return value for id as 42cdb643-0a11-416b-b66b-7bb28a9a20ea and so on.

41686d6564
  • 15,043
  • 11
  • 32
  • 63
AnilKumar
  • 15
  • 4
  • 1
    Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See [this article](https://meta.stackexchange.com/a/5235/248777) for more information. If your question isn't fully answered yet, please provide feedback or [self-answer](http://stackoverflow.com/help/self-answer). – mklement0 Oct 21 '19 at 16:18

2 Answers2

2

To get all the id property values from your text file, use a relatively simple regex with the Select-String cmdlet:

Select-String '^id\s+:\s+(.*)' File.txt | foreach { $_.Matches.Groups[1].Value }

With your sample text, this yields (an array of [string]s):

42cdb643-0a11-416b-b66b-7bb28a9a20ea
d006d69e-8d04-48d7-8806-cdc39c18e679

Explanation:

  • Regex ^id\s+:\s+(.*) finds lines that start with (^) string id, followed by one or more whitespace chars. (\s+), a : and more whitespace, and captures whatever remains on the line (.*) in a capture group ((...)).

  • Select-String passes only matching lines through.

  • The foreach (ForEach-Object) command accesses the details of the regex match ($_.Matches), whose .Groups property contains the capture-group matches; index [1] is the first (and only) capture group's match, and its .Value property contains the matched text, which is the ID of interest.

mklement0
  • 245,023
  • 45
  • 419
  • 492
1

Maybe,

\bid\s+:\s*\b([0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12})\b

would probably work OK, and your desired id value is in the capturing group $1.

Demo


If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Source

Community
  • 1
  • 1
Emma
  • 1
  • 9
  • 28
  • 53