Removing duplicate rows in Notepad++

2019-01-01 16:32发布

Is it possible to remove duplicated rows in Notepad++, leaving only a single occurrence of a line?

标签: notepad++
10条回答
浮光初槿花落
2楼-- · 2019-01-01 16:51

Notepad++

-> Replace window

Ensure that in Search Mode

you have selected Regular expression radio button

Find what:

^(.*)(\r?\n\1)+$

Replace with:

$1

before:

and we think there

and we think there

single line

Is it possible to

Is it possible to

after:

and we think there

single line

Is it possible to

查看更多
伤终究还是伤i
3楼-- · 2019-01-01 16:51

Plugin manager is currently unavailable(does not come with the distribution) for Notepad++ , you must install it manually ( https://github.com/bruderstein/nppPluginManager/releases ) and even if you do, a lot of the plugins are not available anymore (no TextFX) plugin.

Maybe there is another plugin which contains the required functionality. Other than that the only way to do it in NotePad++ is to use some special regex for matching and then replacing (CTRL+F -> Replace tab).

Although there are many functionalities available via Edit menu item (trimming, removing empty lines, sorting, converting EOL) there is no "unique" operation available.

I you have Windows 10 then you can enable Bash (just type Ubuntu in Microsoft Store and follow the instructions in the Description to install it) and use cat your_file.txt | sort | uniq > your_file_edited.txt. Of course you must be in the same working directory as "your_file.txt" or refer to it via it's path.

查看更多
倾城一夜雪
4楼-- · 2019-01-01 16:54

Since Notepad++ Version 6 you can use this regex in the search and replace dialogue:

^(.*?)$\s+?^(?=.*^\1$)

and replace with nothing. This leaves from all duplicate rows the last occurrence in the file.

No sorting is needed for that and the duplicate rows can be anywhere in the file!

You need to check the options "Regular expression" and ". matches newline":

Notepad++ Replace dialogue

  • ^ matches the start of the line.

  • (.*?) matches any characters 0 or more times, but as few as possible (It matches exactly on row, this is needed because of the ". matches newline" option). The matched row is stored, because of the brackets around and accessible using \1

  • $ matches the end of the line.

  • \s+?^ this part matches all whitespace characters (newlines!) till the start of the next row ==> This removes the newlines after the matchd row, so that no empty row is there after the replacement.

  • (?=.*^\1$) this is a positive lookahead assertion. This is the important part in this regex, a row is only matched (and removed), when there is exactly the same row following somewhere else in the file.

查看更多
爱死公子算了
5楼-- · 2019-01-01 16:55

Notepad++ can do this, provided you wanted to sort by line, and remove the duplicate lines at the same time.

You will need the TextFX plugin. This used to be included in older versions of Notepad++, but if you have a newer version, you can add it from the menu by going to Plugins -> Plugin Manager -> Show Plugin Manager -> Available tab -> TextFX -> Install. In some cases it may also be called TextFX Characters, but this is the same thing

The check boxes and buttons required will now appear in the menu under: TextFX -> TextFX Tools.

Make sure "sort outputs only unique..." is checked. Next, select a block of text (Ctrl+A to select the entire document). Finally, click "sort lines case sensitive" or "sort lines case insensitive"

menu layout in n++

查看更多
何处买醉
6楼-- · 2019-01-01 16:56

Search for the Regular Expression: \b(\w+)\b([\w\W]*)\b\1\b

Replace it with: $1$2

Hit Replace Button Until there is no more Matches for the Regular Expression in your file.

查看更多
冷夜・残月
7楼-- · 2019-01-01 17:02

The latter versions of Notepad++ do not apparently include the TextFX plugin at all. In order to use the plugin for sorting/eliminating duplicates, the plugin must be either downloaded and installed (more involved) or added using the plugin manager.

A) Easy way (as described here).

Plugins -> Plugin Manager -> Show Plugin Manager -> Available tab -> TextFX Characters -> Install

B) More involved way, if another version is needed or the easy way does not work.

  1. Download the plugin from SourceForge:

    http://downloads.sourceforge.net/project/npp-plugins/TextFX/TextFX%20v0.26/TextFX.v0.26.unicode.bin.zip

  2. Open the zip file and extract NppTextFX.dll

  3. Place NppTextFX.dll in the Notepad++ plugins directory, such as:
    C:\Program Files\Notepad++\plugins

  4. Start Notepad++, and TextFX will be one of the file menu items (as seen in Answer #1 above by Colin Pickard)

After installing the TextFX plugin, follow the instructions in Answer #1 to sort and remove duplicates.

Also, consider setting up a keyboard shortcut using Settings > Shorcut mapper if you use this command frequently or want to replicate a keyboard shortcut, such as F9 in TextPad for sorting.

查看更多
登录 后发表回答