是否有可能在notepad++中删除重复的行,只留下一行的单一出现?


当前回答

从notepad++版本6开始,你可以在搜索和替换对话框中使用这个正则表达式:

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

什么都不替换。这将在所有重复行中留下文件中最后一次出现的内容。

不需要排序,重复的行可以在文件中的任何地方!

您需要勾选“正则表达式”和“正则表达式”选项。匹配换行符”:

^ 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 matched 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.

其他回答

notepad++的插件管理器目前不可用(不随发行版提供)。你必须手动安装它(https://github.com/bruderstein/nppPluginManager/releases),即使你这样做,很多插件是不可用的(没有TextFX)插件。

也许有另一个插件包含所需的功能。除此之外,在notepad++中做到这一点的唯一方法是使用一些特殊的正则表达式进行匹配,然后替换(Ctrl + F→替换选项卡)。

虽然有许多功能可通过编辑菜单项(修剪,删除空行,排序,转换EOL),但没有“唯一”操作可用。

如果你有Windows 10,那么你可以启用Bash(只需在微软商店中输入Ubuntu,并按照说明安装它),并使用cat your_file.txt | sort | uniq > your_file_editing .txt。当然,你必须在与“your_file.txt”相同的工作目录中,或者通过它的路径引用它。

在7.8版中,您可以在没有任何插件的情况下完成这一点-编辑->行操作->删除连续重复的行。在此工作之前,您必须对文件进行排序,以便将重复的行按连续的顺序放置,但它确实非常有效。

排序选项可在编辑->行操作->排序按…

Notepad + +

->替换window

确保在搜索模式中选择了正则表达式单选按钮

找到:

(*) ^ (r ? (n - 1) +美元

替换为:

$1

之前:

我们认为 我们认为 一行 有可能 有可能

后:

我们认为 一行 有可能

在notepad++ 8.1版本中,有一个特定的命令可以精确地完成这个流行问题的要求。On可以使用菜单命令“编辑>行操作>删除重复行”删除文本文件中的重复行。

不需要安装插件(正如目前接受的答案所建议的那样),也不需要事先对行进行排序,或者像其他答案所建议的那样在Replace对话框中使用regex语法。

notepad++有内置操作:

Edit -> Line Operations -> Sort Lines...
Edit -> Line Operations -> Remove Duplicate Lines

也许它只适用于删除重复行,但我需要通过看到排序工作来看到操作工作。

如果它不工作,问题可能是不同的结束线,我现在遇到的。您可以通过查看->显示符号->显示行结束来检查。 把它替换成相同的。

Click on Search > Replace (or Ctrl + H)
Find what:  \r\n
Replace with: \n
Search Mode: select Extended (\n, \r,...)
Replace All