ChatGPT解决这个技术问题 Extra ChatGPT

Removing duplicate rows in Notepad++

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


C
Colin Pickard

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

To install the TextFX in the latest release of Notepad++ you need to download it from here: https://sourceforge.net/projects/npp-plugins/files/TextFX

The TextFX plugin used to be included in older versions of Notepad++, or be possible to add 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"

https://i.stack.imgur.com/1qnvS.png


Incredibly powerful plugin, despite its "age". Hope they will NEVER remove that one from the standard NPP plugin offer. The guy who thought about all the features in this plug-in, was kind of a "visionary".
More powerful than excel.
What about Notepad++ x64 version? Plugin TextFX x64-version not exists
TextFx is not in the 64 bit version.
@Geograph And there will be no 64 bit plugin of TextFx see this note. Therefore it would be good to know if there is an alternative plugin providing sort and duplicate removal.
q
qyb2zm302

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":

https://i.imgur.com/dY3LCMD.png

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


oh, this one is brilliant, it even deletes empty rows, i'm macroing it this very moment :)
It just removes ALL lines in a file in some cases.
Is there any way to remove the LAST occurrence? This matches all but the last one...
In my case where this solution removed all lines, unchecking the . matches newline did the trick.
@SerG In some cases it didn't work for me also, but when I removed "matches newline" it did :)
P
Peter Mortensen

If the rows are immediately after each other then you can use a regex replace:

Search Pattern: ^(.*\r?\n)(\1)+

Replace with: \1


Maybe others have had luck with this, but for me ^(.*\n)\1 results in "Cant find the text"
@b1naryatr0phy make sure you have "Search Mode" set to "Regular expression", I also updated the pattern so that it can handle windows style line endings
notepad++ has a light regex engine, it dosen't permit advanced functios, not even the "? or \r\n" as it only works on a single line and you use $ for the \r\n characters
this eliminates one by one. You must repeat it many times. I wonder why \n+ -> \n does not work (thought it reports many replacements)
@Val, if you make the back-reference part of the match a group with 1-or-more matches required, the pattern will match N contiguous duplicate lines at a time: ^(.*\r?\n)(\1)+
P
PeterK

In version 7.8, you can accomplish this without any plugins - Edit -> Line Operations -> Remove Consecutive Duplicate Lines. You will have to sort the file to place duplicate lines in consecutive order before this works, but it does work like a charm.

Sorting options are available under Edit -> Line Operations -> Sort By ...


There's also now an option for Edit -> Line Operations -> Remove Duplicate Lines which eliminates the need to sort.
Whats the difference between "Remove Duplicate Lines" and "Remove Consecutive Duplicate Lines"
First option should remove all but one of each matching line in a document (so a, a, b, a, c would become a, b, c). Second option should only remove lines that are repeated immediately after a matching line (a, a, b, a, c would become a, b, a, c).
P
Peter Mortensen

Notepad++

-> Replace window

Ensure that in Search mode you have selected the 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


Isn't the file required to be sorted for this to work?
P
Peter Mortensen

If you don't care about row order (which I don't think you do), then you can use a Linux/FreeBSD/Mac OS X/Cygwin box and do:

$ cat yourfile | sort | uniq > yourfile_nodups

Then open the file again in Notepad++.


Doesn't work on Windows 7. 'cat' is not recognized as an internal or external command, operable program or batch file.
@Iain Elder: cat is a standard Unix utility, which is why this answer specifies that it works on linux, FreeBSD, and MacOSX. The answer also suggests Cygwyn: This is a windows program that gives you a unix style shell, and with it, cat. Long story short (too late!): Win 7 needs Cygwin to do this.
In windows you have powershell: cat yourfile | sort -Unique
These are good examples of "the gratuitous use of cat". Forget about the cat utility and just use file redirection thusly: sort < yourfile | uniq > yourfile_nodups
@scott8035, I agree that cat is of no use for running that command, but I find it often helpful to start with cat when figuring out a long sequence of non-obvious commands, like cat file | sed ... | sed ... | sed ... and so on. So I'd say that there might be reasons for using cat. Of course cat can be removed at the end, but some are too lazy for that.
d
divenex

As of Notepad++ version 8.1, there is a specific command to do precisely what this popular question asks. On can remove duplicated rows in a text file with the menu command Edit > Line Operations > Remove Duplicate Lines.

There is no need to install a plugin (as the currently accepted answer suggests), or sort the lines beforehand, or use the regex syntax in the Replace dialogue as other answers suggested.

https://i.stack.imgur.com/BWJZJ.png


This is perfect solution and saving my lots of time to go to Excel and perform Einstein based operations.
This works perfectly out of the box. Should be the accepted answer IMHO
Excellent answer and easiest approach. It worked perfectly for me.
C
Community

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.

Download the plugin from SourceForge: http://downloads.sourceforge.net/project/npp-plugins/TextFX/TextFX%20v0.26/TextFX.v0.26.unicode.bin.zip Open the zip file and extract NppTextFX.dll Place NppTextFX.dll in the Notepad++ plugins directory, such as: C:\Program Files\Notepad++\plugins 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.


In notepad++ 7.6, the plug-in should be added to C:\Users\<your_user>\AppData\Local\Notepad++\plugins\NppTextFX . Other than that this still works fine.
S
SaPropper

As of now, it's possible to remove all consecutive duplicate lines with Notepad in-built functionality. Sort the lines first:

Edit > Line Operations > "Sort lines lexicographically",

then

Edit > Line Operations > "Remove Consecutive Duplicate Lines".

The regex solution suggested above didn't remove all duplicate lines for me, but just the consecutive ones as well.


D
Donald

You may need a plugin to do this. You can try the command line cc.ddl(delete duplicate lines) of ConyEdit. It is a cross-editor plugin for the text editors, including Notepad++.

With ConyEdit running in background, follow the steps below:

enter the command line cc.ddl at the end of the text. copy the text and the command line. paste, then you will see what you want.

https://i.stack.imgur.com/EtPOM.gif


P
Peter Mortensen

Search for the regular expression: \b(\w+)\b([\w\W]*)\b\1\b

Replace it with: $1$2

Hit the Replace button until there are no more matches for the regular expression in your file.


Created a test file to try this, but the regular expression did not work reliably to get the job done.
Would you please provide an example that fails so I can improve my answer?
Remove duplicates leaving also the original row number position of other text, I like this solution
M
Manohar Reddy Poreddy

None worked for me.

A solution is:

Replace

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

with

\1

Created a test file to try this, but the regular expression did not work reliably to get the job done.
For all my data, it worked fine.I forgot what my solution was. Add more details where it failed so that other people might improve this regex.
I created a file so each line had a integer between 0-999 on it, in random order, sometimes with duplicates. It didn't remove most of the duplicates, and didn't remove any duplicates there were not sequential.
Please do provide 2 examples for working and for not-working ones. It will help someone.
why ^(.*)\s+(\r?\n\1\s+)+$ and not ^(.*)\s*(\r?\n\1\s*)+$ ?
P
Peter Mortensen

The 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.

If 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 its path.


α
αғsнιη

Whether the file is sorted or not, you can use below regex to remove duplicates in anywhere occurred in your file.

Find what: ^([^\r]*[^\n])(.*?)\r?\n\1$
Replace with: \1\2
Search Mode:

"Regular expression"

Check the ". matches newline" option

do "Replace All" as many time as possible until you see "0 occurrences were replaced"


I like this because you're not forced to sort the contents of the file first. It also can be used on any text editor that supports Perl regex.
R
RJFalconer

Extending the top answer, you can also use a 2nd lookahead to find rows that are almost duplicates of other rows.

^(\s*(<PackageReference Include=".*" Version=).*)$\s+?^(?=.*^\2.*$)

Here I'm after multiple references to the same <PackageReference Include=".*" string, regardless of its version.

Test data

<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" />

See a breakdown of what the regex terms mean and try with your own data on this regex101 share.


h
haykp

Difficult to do this in NPP. Better way is following:

Download cygwin utility, it is simple Linux terminal under windows. It allow to execute any Linux command in Windows. And you have sort -u there.


Why is it difficult? Have you seen other answers? What's wrong with them?