Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decipher this sed one-liner

Tags:

sed

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.

like image 617
Robottinosino Avatar asked Feb 20 '23 01:02

Robottinosino


1 Answers

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

like image 84
tripleee Avatar answered Feb 27 '23 02:02

tripleee