SLS context

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

I am sorry I am revisiting this but where did you get $found from? Also how does this get everything after info2. I am sorry if I am not understanding the concept here.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

It is created when the target line is found.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

Ok lets start from the begaining here because I am missing something. I do a sls and I want everything after the string I am selecting. Normally I would use context but the list may be big and I don't want to guess. I am trying to understand where the variable $found comes from? The code below gets me the [infor2].

  1. line = sls -path .\Settings.txt -Pattern  '[Info2]'
  2.  
  3. foreach ($element in $line.line)
  4. {
  5.     if ($element -eq '[Info2]') { $element }
  6.    
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

Set a flag when the element is found and use the flag to cause all following lines to be output. The example I posted is very explicit.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

Ok what you explicitly gave doesn't work. Or I misunderstanding here. Please see the below.

  1.  
  2. $line = sls -path .\Settings.txt -Pattern '[info2]'
  3.  
  4. foreach ($element in $line.line)
  5. {
  6.     if ($element -match '[info2]') { $found = $true }
  7.     if ($found) { $element }
  8. }
OUTPUT:
  1. [Info]
  2. 3= data1
  3. 2= data2
  4. 4 =data3
  5.  
  6. [info2]
  7. 1 =data1
  8. 2 = data2
  9. 3 =data3
  10.  
  11.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

What doesn't work? It outputs everything after the key line is found which is what you asked for,
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

No it is outputting everything.
  1. [Info]
  2.  3= data1
  3.  2= data2
  4.  4 =data3
  5.  
  6.  [info2]
  7.  1 =data1
  8.  2 = data2
  9.  3 =data3
instead of just outputting everything under [info2]
  1. 1 = data1
  2. 2 = data2
  3. 3 = data3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

You said "everything after the line.

I posted the INI file library for you which can extract everything in a section in one command. I recommend using that.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SLS context

Post by jvierra »

The following code will work better for what you are trying to do:

https://gallery.technet.microsoft.com/E ... 67?redir=0
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: SLS context

Post by sekou2331 »

I see what you meant by explicit. I just couldn’t see it. Still working on my understanding with certain concept when comes to scripting. Well if anyone has the same issue and can understand here is the script in full.



$line = sls -path .\Settings.txt -Pattern '[info2]'
$checkForline = $false

$line | %{
if ($checkForline) { $_.line }
if ($_.line -eq '[info2]')
{
$checkForline = $true


}
}
$_
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked