John Wade
John Wade

Reputation: 23

Replace or remove everything after the first few lines

I've got a lot of long XML files and I want to replace everything in them after the 4th line, I've been looking around and can't find anything with the expressions to select everything after the 4th line.

<?xml version="1.0" encoding="UTF-8"?>
<CMapData>
<name>garage_door_locked</name>
<parent />
select everything from here to the end of the file

Is such a thing possible with regular expressions? Also, the first 4 lines are entirely different in each of my files. I just need to skip them somehow.

Upvotes: 0

Views: 426

Answers (2)

Toto
Toto

Reputation: 91430

  • Ctrl+H
  • Find what: \A(?:.*\R){4}\K[\s\S]+\z
  • Replace with: LEAVE EMPTY
  • SELECT Regular expression
  • UNTICK . matches newline
  • Replace all

Explanation:

\A          # beginning of file
    (?:         # non capture group
        .*          # 0 or more any character but newline
        \R          # any kind of linebreak (i.e. \r, \n, \r\n)
    ){4}        # end group, must appear 4 times
    \K          # forget all we have seen until this position
    [\s\S]+     # 1 or more any character
\z          # end of file

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521339

You may try the following find and replace, in regex mode:

Find:    (?:.*?\R){4}
Replace: (empty)

The above logic will match the first 4 lines. We then replace with nothing to effectively remove them. Here is a working demo.

Upvotes: 1

Related Questions