Home > Blockchain >  I want to capture a set of 5 consecutive lines from a file where after my condition matches by using
I want to capture a set of 5 consecutive lines from a file where after my condition matches by using

Time:11-18

$ourfilesdata = Get-Content "P:\myfiles\details.txt"

foreach ($i in $ourfilesdata )
    {
    if ( $i -match '\Mobile\b') {continue)
      {
      Write-Output "$i"
      }
    }

**My input is like 50 lines **

aaaaaaa bbbbb Request Mobile Sim datacard internet ccccccc dddddddd fffffff

Output

mobile sim datacard internet

Note :- These input lines are horizontal fashion in my file

CodePudding user response:

I don't quite understand what you're trying to accomplish, but see if this is what you're looking for:

#Current working directory
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$MainPath = $PSScriptRoot  = "\"

$ourfilesdata = $MainPath   "Randomc.txt"

$ourfilesdata

$search = '\\Mobile\\b'
$GetLine = Get-Content $ourfilesdata | Select-String $search

$GetLine.LineNumber

$GetLine

CodePudding user response:

Select-String "P:\myfiles\details.txt" -Pattern 'Request\b' -Context 0,5 | Foreach-Object { $.Line,$.Context.PostContext}

This will 100% works and thanks me later :-)

  • Related