2

I have one file in which i want to search a text and then i need to find start and end block. Then need to copy to another file. I have multiple statements in the same file.

below is an example.

1: Start
2: hello 
3: Hello world 
4: Good Morning 
5: End

I want to search "Good Morning" and then i want to copy text between start and end block to new file.

2
  • What have you tried so far? Also do you want the numbers included in your search or no? Commented Nov 12, 2018 at 19:07
  • 1
    IF - and that is a big IF - your text is formatted as shown all the time, then take a look at the -Context parameter of Select-String. that accepts one or two [int]s for "lines before, lines after". so -Context 3,1 will grab the 3 lines before the matching line AND the 1 line after it. [grin] ///// take a look at Get-Help Select-String -Parameter Context for more info. Commented Nov 12, 2018 at 19:52

2 Answers 2

1

With a regular expression with lookarounds you can find the match
(see the regex live with different escaping \=` )

With your text in a file .\sample.txt, this snippet:

#requires -Version 3.0
[regex]::match((Get-Content .\Sample.txt -raw),"(?sm)(?<=^Start`r?`n).*?Good Morning.*?(?=`r?`nEnd)").Value
#                                                    \  lookbehind /                   \lookahead/

returns

hello
Hello world
Good Morning
Sign up to request clarification or add additional context in comments.

Comments

0

The following will get you what you want. First I grabbed the contents of the txt file and put it in the $doc variable. Then using powershell's native "text" search feature I look for the string containing "Good Morning" and if that is true then using regex I grab all contents between the start and end text and create a new txt file with those contents. Below is the code.

$doc = Get-Content C:\Scripts\test.txt

      if(Select-String -InputObject $doc -Pattern "Good Morning" -SimpleMatch){
        $contents = [regex]::Match($doc,'(?is)(?<=\b\d: Start\b).*?(?=\b\d: End\b)')
            New-Item -Path C:\Scripts -Name newtest.txt -ItemType File -Value $contents
      }
      Else{
         Write-Host "Nothing Found"
      }

4 Comments

I tried below code. it generates output file but its blank. can we add like command instead of is start end? $doc = Get-Content "C:\Users\a154499\Desktop\testPS.txt" if(Select-String -InputObject $doc -Pattern "dwsn14626" -SimpleMatch){ $contents = [regex]::Match($doc,'(?is)(?<=\b\d: Schedule\b).*?(?=\b\d: End\b)') New-Item -Path C:\tmp1 -Name newtest.txt -ItemType File -Value $contents } Else{ Write-Host "Nothing Found" }
It is not working for me. i have many recursive entries in the file. will this script help to show multiple section in file?
It worked if i have only one occurrence but not working in my case because i have multiple occurrence of schedule and end statements. $doc = Get-Content "C:\testPS.txt" if(Select-String -InputObject $doc -Pattern "DWSN14624"){ $contents = [regex]::Match($doc,'(?sm)(?=SCHEDULE).*?DWSN14624.*?(?=END).*') New-Item -Path C:\tmp1 -Name newtest.txt -ItemType File -Value $contents } Else{ Write-Host "Nothing Found" }
Separating the trigger Good Morning from the Start,End markers could lead to false positives in a more complex scenario than originally given. @Saurabh Did I guess right the leadinng numbers colon are not part of the file? You should append a more real life example to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.