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


当前回答

在NPP很难做到这一点。 更好的方法是:

下载cygwin实用程序,这是一个简单的Linux终端在windows下。 它允许在Windows中执行任何Linux命令。 这里是排序-u。

其他回答

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

扩展顶部的答案,还可以使用第二个前向查找几乎与其他行重复的行。

^(±s * (< PackageReference包括=”。* * *版=)。*)$±s +?^(?=.*^\ 2 . *美元)

这里我在多次引用同一个<PackageReference Include="。字符串,不管它的版本。

测试数据

<PackageReference Include="Package1" Version="2.2.1" />

    <PackageReference Include="Package1" Version="2.2.1" /> // Match
<PackageReference Include="Package1" Version="2.2.2" />

<PackageReference Include="Package2" Version="5.1" /> // Match
<PackageReference Include="Package2" Version="5.2" />

<PackageReference Include="Package3" Version="2.2.1" /> // No match
<PackageReference Include="Package4" Version="2.2.1" />

查看regex术语含义的详细说明,并尝试使用这个regex101共享中的您自己的数据。

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

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

替换为:$1$2

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