是否有可能在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++版本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.

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

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

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”相同的工作目录中,或者通过它的路径引用它。

搜索正则表达式:\b(\w+)\b([\w\ w]*)\b\1\b

替换为:$1$2

点击“替换”按钮,直到文件中不再匹配正则表达式为止。

如果行是紧挨着的,那么你可以使用正则表达式替换:

搜索模式:^(.*\r?\n)(\1)+

替换为:\1