Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:53:28+00:00 2026-06-06T12:53:28+00:00

I want to remove duplicate lines from a file, without sorting the file. Example

  • 0

I want to remove duplicate lines from a file, without sorting the file.

Example of why this is useful to me: removing duplicates from Bash’s $HISTFILE without changing the chronological order.

This page has a one-liner to do that:

http://sed.sourceforge.net/sed1line.txt

Here’s the one-liner:

sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

I asked a sysadmin and he told me “you just copy the script and it works, don’t go philosophising about this”, which is fine, so I am asking here as it’s a developer forum and I trust people might be like me, suspicious about using things they don’t understand:

Could you kindly provide a pseudo-code explanation of what that “black magic” script is doing, please? I tried parsing the incantation in my head but especially the central part is quite hard.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:53:30+00:00Added an answer on June 6, 2026 at 12:53 pm

    I’ll note that this script does not appear to work with my copy of sed (GNU sed 4.1.5) in my current locale. If I run it with LC_ALL=C it works fine.

    Here’s an annotated version of the script. sed basically has two registers, one is called “pattern space” and is used for (basically) the current input line, and the other, the “hold space”, can be used by scripts for temporary storage etc.

    sed -n '                    # -n: by default, do not print
        G                       # Append hold space to current input line
        s/\n/&&/                # Add empty line after current input line
        /^\([ -~]*\n\).*\n\1/d  # If the current input line is repeated in the hold space, skip this line
                                # Otherwise, clean up for storing all input in hold space:
        s/\n//                  # Remove empty line after current input line
        h                       # Copy entire pattern space back to hold space
        P                       # Print current input line'
    

    I guess the adding and removal of an empty line is there so that the central pattern can be kept relatively simple (you can count on there being a newline after the current line and before the beginning of the matching line).

    So basically, the entire input file (sans duplicates) is kept (in reverse order) in the hold space, and if the first line of the pattern space (the current input line) is found anywhere in the rest of the pattern space (which was copied from the hold space when the script started processing this line), we skip it and start over.

    The regex in the conditional can be further decomposed;

    ^    # Look at beginning of line (i.e. beginning of pattern space)
    \(   # This starts group \1
    [ -~] # Any printable character (in the C locale)
    *     # Any number of times
    \n    # Followed by a newline
    \)   # End of group \1 -- it contains the current input line
    .*\n # Skip any amount of lines as necessary
    \1   # Another occurrence of the current input line, with newline and all
    

    If this pattern matches, the script discards the pattern space and starts over with the next input line (d).

    You can get it to work independently of locale by changing [ -~] to [[:print:]]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove duplicate records from a table without using temp table how
I want to remove duplicate entries from a text file, e.g: kavitha= Tue Feb
I want to remove duplicate rows return from a SELECT Query in Postgres I
I want to remove duplicates from my DataTable so I'm using DataTable.AsEnumerable().Distinct(DataRowComparer.Default) but it
I have NSArray and I want to remove duplicates from it. I know that
I want to know is there any way that I can remove duplicates from
I want to remove all spaces from a string in Lua. This is what
I want to remove from an input file every word .xx that appears on
I want to split the given string and remove the duplicate from that string.
Given an input file of text lines, I want duplicate lines to be identified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.