How to remove every odd line in Notepad++?

29

8

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?

Nithin N

Posted 2017-09-01T07:56:44.857

Reputation: 285

what do you mean by odd line? Do you mean like to remove the odd (unwanted) word from the entire document? – antzshrek – 2017-09-01T08:00:08.947

4Every odd numbered lines like 1,3,5,7,9,11,13,15....so on. – Nithin N – 2017-09-01T08:04:48.863

1Try replace with new line (regexp): [\n.+\n] – Art Gertner – 2017-09-01T08:05:22.143

@ArtGertner i tried with the expression its not working. – Nithin N – 2017-09-01T08:10:10.923

Sorry, I never really use Notepad++, My previous example was tested on Sublime Text. I forgot that its a Windows software. If you are on windows then likely you will need \n,+\r\n – Art Gertner – 2017-09-01T08:30:33.467

1I d record a stupid macro: down arrow, shift-down arrow, del. – chingNotCHing – 2017-09-01T08:45:30.683

Just out of curiosity, how would a macro detect the end of the document and stop? – Art Gertner – 2017-09-01T08:57:43.643

@ArtGertner the button >> Run until the end of file – chingNotCHing – 2017-09-01T11:22:18.730

Answers

24

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

Máté Juhász

Posted 2017-09-01T07:56:44.857

Reputation: 16 807

It almost works, but it is not editing the first set of odd numbered lines, say from 1 to 15. – Nithin N – 2017-09-01T09:42:12.120

Change the screenshot with more transperacy of that replace window. – Biswapriyo – 2017-09-01T10:59:20.650

11Please 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. – Aurel Bílý – 2017-09-01T11:33:50.410

@AurelBílý That's true, however Notepad++ only runs on windows, so portability here isn't much of an issue. – Rob – 2017-09-03T01:29:39.477

18

  • 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

Toto

Posted 2017-09-01T07:56:44.857

Reputation: 7 722

@NithinN: Be aware that accepted answer doesn't work with linux files (ie. linebreak \n) – Toto – 2017-09-01T10:22:12.043

8

  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!

UserF40

Posted 2017-09-01T07:56:44.857

Reputation: 181

1

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

Enter image description here

Art Gertner

Posted 2017-09-01T07:56:44.857

Reputation: 6 417

It seems to me this to remove even lines (2,4,6...), not odd ones. – Máté Juhász – 2017-09-01T08:36:06.743

1That 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 – Art Gertner – 2017-09-01T08:37:10.157

@ArtGertner should be \r\n.+\r\n replaced with \r\n – chingNotCHing – 2017-09-01T08:42:05.280

@chingNotCHing, good one! I've updated my answer – Art Gertner – 2017-09-01T08:49:31.573

@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 – 2017-09-01T09:46:08.130