How to remove a specific, duplicated line within a file?

neXus

I'm looking for a way to remove one specific line from a bunch of files, but only if it occurs more than once in that file. Other lines should be kept, even if they are duplicates.

For example, a file like this where I would like to remove the duplicates of AAA

AAA
BBB
AAA
BBB
CCC

should become

AAA
BBB
BBB
CCC

I guess I should use sed but I have no idea how to write the command.

Stéphane Chazelas

With GNU sed:

sed '0,/^AAA$/b;//d'

That is, let everything through (b branches off like a continue) up to the first AAA (from the 0th line (that is even before the first line) and the first one matching /^AAA$/ (which could be the first line)), and then for the remaining lines, delete every occurrence of AAA (an empty // pattern reuses the last pattern).

GNU sed is needed for the 0 address (and the ability to have other commands after the b one in the same expression, though that could be easily worked around in other implementations by using two -e expressions)

With awk:

awk '$0 != "AAA" || !n++'

(or for a regexp pattern: awk '!/^AAA$/ || !n++')

a shorthand for:

awk '! (&0 == "AAA" && count > 0) {print; count++}'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove line with specific string from file

remove duplicated pattern/entries within each field in CSV file

How to remove a blank line in text file after deleting a specific line?

How to delete (remove) a specific line of a text file based on the line number?

R: How to remove duplicated entry across columns within each row

Remove duplicated element into a file

How to remove commas in the middle of a specific line in a file in linux/unix

How to remove line matching specific pattern from a file

How to remove line containing a specific string from file?

How do I remove a specific line from a text file in python

How to remove every line starting with specific string from log file?

How to remove a line from csv file if it contains a specific word

How to remove all the duplicated words on every line using Notepad++?

Remove Line from String within txt file

Remove duplicated line dividers with jQuery

How to append a string to one specific line and remove a string from another specific line in a file?

how to remove duplicated data?

How to remove duplicated checkboxes

Count and show how many times a line is duplicated in a file

How to delete selected duplicated line from file using perl script

How to remove a specific line that contains a specific string?

Remove duplicated word from a file

Remove duplicated strings within Notepad++

How to remove duplicate values within one line?

Remove lines with specific line number specified in a file

Remove specific line from txt file

Remove a specific line from a .txt file

Powershell How to remove specific characters within a text

How to remove text specific text within link