how can i find the line number for a search in file ?

i have a file  with the content :

---- tata -----
blabl
blablba
blibli
--- end tata ---

---- toto ---
blibli
blolo
--- end toto ---


question :

how can i recover the content of the file in between "tata" and "end tata"  ?
actually i can not find a way to get the line number.

thank you in advance.

toshi
Erwin PombettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
Sorry, I'm confused, You either want the content, or the line number? Which one do you prefer?

Here's the solution for the content:

$filePath = "C:\users\Kimputer\Desktop\test.txt"
$text = [IO.File]::ReadAllText($filePath)
$myarray =  % { [regex]::matches( $text , '(?mis)(?<=---- tata -----)(.*?)(?=--- end tata ---)' ) } | select -expa value
Write-Output $myarray

Open in new window

0
Erwin PombettAuthor Commented:
hello ,

i'd like to recover the line number.
because i'd like to recover the lines in between as follows :

for example :
    -  $linesToTreat = $aFileContent | Select-Object -Index($beginLineNo..$endLineNo)

thank you.

Toshi
0
KimputerCommented:
Try something like this:
$reader = [System.IO.File]::OpenText("C:\test.txt")
$counter = 0
try {
    for(;;) {
        $counter= $counter + 1
        $line = $reader.ReadLine()
        if ($line -eq $null) { break }
        if ($line -eq "---- tata -----") { Write-Output "start" $counter
		$beginLineNo = $counter
		}
        if ($line -eq "--- end tata ---") { Write-Output "end" $counter
		$endLineNo = $counter
		}

    }
}
finally {
    $reader.Close()
}

write-output $beginLineNo
write-output $endLineNo

Open in new window


Add or substract 1 to in- or exclude the actual start and end triggers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
How about this
$aFileContent = get-content file.txt
$start, $end = $aFileContent | select-string 'tata -----' | select -Expand LineNumber -First 2

Open in new window

assuming each section is existing only once in the file.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.