0

is there a way to select a string pattern, and get also the line after the pattern.

my metadata look like :

"t": "d",
      "n": "00001212",
        "22.06.2031",
        "",
        "",
        "batman",

my codeline looks like:

$contentNameFolder= $content__ | Select-String -Pattern '"n": * ' 

my output looks like :

"n": "00001212",

what i want to get is:

"n": "00001212",
        "22.06.2031",

i was trying to add into my code -context 1,2 or something like that but this dont worked for me.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
du7ri
  • 67
  • 1
  • 10
  • 1
    When you carefully (re-)read the help topic for [Select-String](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-string?view=powershell-7.1) you may notice the parameter `-Context` which gives you the opportunity to include lines before and/or after the match. – Olaf Jul 28 '21 at 07:33
  • `($content__ -join "\`n" | Select-String -Pattern '"n":.*\s.*' ).Matches.Value` – Daniel Jul 28 '21 at 08:25
  • Have you tried: `-Context 0,1` ? That should print 0 lines before and 1 line after the match. – Manuel Batsching Jul 28 '21 at 08:30
  • presuming your `$content__` is an array of strings, the pattern you used will ONLY return one string ... the one that contains `"n":`. if you want to add more lines, then you will either need to `[A]` use the `-Context` parameter, `[B]` use multiple regex patterns, OR `[C]` bring in the lies as a multiline string and use a regex pattern that covers more than one line of that string. – Lee_Dailey Jul 28 '21 at 08:38

1 Answers1

0

You have been close. As you have already seen Select-String has the -Context parameter which allows you to specify the lines before and after the match that should be displayed.

If you pass in an array with two integers the first number determines the lines before the match and the second number the lines after the match (See: Select-String | Microsoft Docs).

If you don't want any lines before the match to display, set the first integer to 0.

$contentNameFolder= $content__ | Select-String -Pattern '"n": * ' -Context 0,1

In your example this will give you the following result:

"n": "00001212",
        "22.06.2031",
Manuel Batsching
  • 3,406
  • 14
  • 20