-2

I have quite a few text files that have text in them that I want to remove. The bottom line is consistent but there are two lines about it that are different almost every time. For example:

This is the beginning of a junk entry but a different each time
This is junk
-- END OF ENTRY --

Is it possible to find the line that says '-- END OF ENTRY --', whether using sed, awk, grep or whatever and remove it and the two previous lines?

I have found a way to replace the text on the bottom line using sed with s/ and /g but I can't search and replace the other two as they change.

Can anyone help?

Thanks

Tried sed with search and replace (s/ and /g) but this doesn't help as the two previous lines change all the time.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    Please edit your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? – Cyrus Apr 16 '23 at 18:27
  • have you tried a web search on something like `delete line and previous 2 lines`? – markp-fuso Apr 16 '23 at 18:30

1 Answers1

0

With , reading the whole file in memory:

$ cat file
foo
bar
base
This is the beginning of a junk entry but a different each time
This is junk
-- END OF ENTRY --
qux
$ perl -0777 -pe 's/[^\n]+\n[^\n]+\n-- END OF ENTRY --//' file
foo
bar
base

qux

The regular expression matches as follows:

Node Explanation
[^\n]+ any character except: '\n' (newline) (1 or more times (matching the most amount possible))
\n '\n' (newline)
[^\n]+ any character except: '\n' (newline) (1 or more times (matching the most amount possible))
\n '\n' (newline)
-- END OF ENTRY -- -- END OF ENTRY --
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223