32

I am trying to edit a document of 20000 lines. I have to remove every odd line, say for example:

Apple
Monkey
Orange
Lion
fruit
animal

How can I remove every odd line's characters in the document?

8
  • what do you mean by odd line? Do you mean like to remove the odd (unwanted) word from the entire document?
    – antzshrek
    Sep 1, 2017 at 8:00
  • 4
    Every odd numbered lines like 1,3,5,7,9,11,13,15....so on.
    – Nithin N
    Sep 1, 2017 at 8:04
  • 1
    Try replace with new line (regexp): [\n.+\n] Sep 1, 2017 at 8:05
  • @ArtGertner i tried with the expression its not working.
    – Nithin N
    Sep 1, 2017 at 8:10
  • 1
    I d record a stupid macro: down arrow, shift-down arrow, del. Sep 1, 2017 at 8:45

4 Answers 4

29

You can do it with find and replace:

  • Open the replace dialog (Ctrl + H)
  • Select "Regular expression"
  • Find what: .+\r\n(.+(\r\n|$))
  • Replace with:
    $1
  • Press "Replace All"

Notes:

  • Depending on your OS you need to use \r (old Mac), \n (Unix, OS X) or \r\n (Windows) to match end of line, or just use \R which should work everywhere (thanks for @Aurel Bílý)
  • \n|$ is needed to have correct result even at the end of the file
  • By default, Notepad++ replaces from actual cursor position to the end of file. Make sure to place your cursor to the beginning of file.
    • (You could also check "wrap around", but in that case it will first delete the line your cursor is in, instead of really looking for an odd line.)

Enter image description here

6
  • It almost works, but it is not editing the first set of odd numbered lines, say from 1 to 15.
    – Nithin N
    Sep 1, 2017 at 9:42
  • Change the screenshot with more transperacy of that replace window.
    – Biswapriyo
    Sep 1, 2017 at 10:59
  • 11
    Please note that \r as a line ending is very rare – it was indeed used in Mac OS, but the classic, pre-OS-X versions. In OS X an onwards, line endings are \n, like any sane Unix. Also \R is better for matching any line ending. Sep 1, 2017 at 11:33
  • @AurelBílý That's true, however Notepad++ only runs on windows, so portability here isn't much of an issue.
    – Rob
    Sep 3, 2017 at 1:29
  • "Invalid regular expression"
    – user624722
    Jan 4, 2021 at 9:51
19
  • Ctrl+H
  • Find what: .+\R(.+)
  • Replace with: $1
  • Replace all

Explanation:

.+      : 1 or more any character but newline
\R      : any kind of linebreak (ie. \r, \n, \r\n)
(       : start group 1
  .+    : 1 or more any character but newline
)       : end group 1
  • Check regular expression
  • DO NOT CHECK . matches newline

Result for given example:

Monkey
Lion
animal
2
  • @NithinN: Be aware that accepted answer doesn't work with linux files (ie. linebreak \n)
    – Toto
    Sep 1, 2017 at 10:22
  • 1
    This should be the accepted answer.
    – user624722
    Jan 4, 2021 at 9:52
10
  1. Open the file and put your text cursor at the start of the first line.
  2. Menu → MacroStart Recording
  3. Press the End key on keyboard
  4. Shift + Home, then backspace
  5. Down
  6. Backspace
  7. Down
  8. Menu → MacroStop Recording
  9. Menu → MacroRun a Macro Multiple Times → Tick Run until the end of file
  10. Click Run

Basically perform the action once to remove an odd line, then get the program to repeat the action to the end of the file. This can be used to solve many problems!

1

I just checked on my colleague's PC who actually uses Notepad++. Try this:

Enter image description here

5
  • It seems to me this to remove even lines (2,4,6...), not odd ones. Sep 1, 2017 at 8:36
  • 1
    That is correct. However removing even lines is easier, and the problem can be resolved by adding an empty line at the beginning of the file Sep 1, 2017 at 8:37
  • @ArtGertner should be \r\n.+\r\n replaced with \r\n Sep 1, 2017 at 8:42
  • @chingNotCHing, good one! I've updated my answer Sep 1, 2017 at 8:49
  • @ArtGertner As you pointed out it works for even lines only. But for large set of data it is not editing for first set of data say from 1-15th line.
    – Nithin N
    Sep 1, 2017 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .