-2

Could anyone assist me in finding a simple way to find a string in a text file then deleting 10 lines above that matched text using powershell?

For instance, I have a text file along these lines (with multiple instances of similar text I want to find):

=============start============
random text
random text
random text
random text
random text
string I want to find
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============

So in this example, I want to search for every instance of "String I want to find" and when found delete the X lines above the matched text, outputting the rest to another .txt file. The number of lines above is always the same.

I have tried to match the text between the two strings as follows:

(Get-Content -LiteralPath $Path\processing\data.txt) -replace     '$matchstart*$matchend','' |out-file $Path\processing\test.txt

However, this doesn't seem to give me the output I was hoping for (doesn't remove anything).

This also works, but potentially will cause issues because the start string often appears multiple times and could lead to me deleting more than I want to.

$str = Get-Content $Path\processing\pfxdetailpre.txt | out-string
$start = $str.indexOf($nokeystart) + 1
$end = $str.indexOf($nokeyend, $start)
$length = $end - $start
$result = $str.substring($start, $length)

The following seems to work great to delete my string and the line before. I can't figure out how to edit it to delete X more lines above though.

gc $file | %{
$a=$b=$null
}{
if($a){$a}
$a=$b
$b=$_
if($b -match $key){$a=$b=$null}
}{
if($b -notmatch $key){
if($a){if($b){"$a`n$b"}else{$a}}elseif($b){$b}
}
} | out-file $newfile
Simon K
  • 1
  • 1
  • 3
  • Are you able to update the question with what you have already tried? – arco444 Mar 23 '17 at 15:36
  • Stack Overflow is not a code writing service, it's expected that you attempt to code this yourself. I would suggest you do some research on your issue (maybe try the search box at the top of the page) and make an attempt at writing some code yourself. If/when you come across any issues with your code ask again and explain what you have tried, and why it did not work for you. See [How to Ask](http://stackoverflow.com/questions/how-to-ask) for help with asking a great question. – henrycarteruk Mar 23 '17 at 15:37
  • The code I have at the moment was trying to match the start string and the end string around the text as follows: (Get-Content -LiteralPath $Path\processing\data.txt) -replace '$matchstart*$matchend','' |out-file $Path\processing\test.txt The command runs but it doesn't remove any of the text so I know it isn't matching as expected. – Simon K Mar 23 '17 at 15:59
  • `-replace` uses RegEx - to match anything inbetween you should use `'$matchstart.*$matchend` a single asterisk only matches any number of the previous, with your version the last char in `$matchstart` –  Mar 23 '17 at 16:14

1 Answers1

0

Okay, so using LotPings suggestion I have the following code that works for me. I couldn't get it working with '$matchstart.*$matchend so I just used the raw values which don't change anyway:

So, my file kinda looks like this. I know what the start/end looks like, but I only want to delete it if it contains a certain string:

=============start============
random text
random text
random text
string I want to find
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============
=============start============
random text
random text
random text
=============end============

I found the following code works great!

$file = Get-Content C:\test1.txt -Raw
$result = $file | Select-String -Pattern '(?smi)(=============start============\s+string I want to find.*?=============end============)'
$result.Matches.Value | Out-File -FilePath C:\processing.txt
$file -replace '(?smi)(=============start============\s+string I want to find.*?=============end============)' | Out-File -FilePath C:\configfinal.txt

I would still love to hear how others would approach this. I am very new to powershell as I have only been learning it for a couple of weeks and this is the first task I wasn't able to easily figure out by myself.

Simon K
  • 1
  • 1
  • 3