I found a one-line
sed command on this site which removes duplicate entries in
$PATH. There is one concept that I can't get my mind around.
It uses saved matches in the match section to detect the duplicate, and then replaces the match with
In the example below, I can't understand why
\1 is not always
The test case obviously shows that the
\1 match progresses further down the pattern space, but I can't understand why.
sed script (
p #debug :b s/[:;]\([^:;]*\)\([:;].*\)[;:]\1/;y\1yx\2x/p s/[yx]//g #debug remove the field indicators for the next pass s/;/:/g #debug tb s/^\([^:]*\)\(:.*\):\1/\1\2FixedFirst/ aDone
Test command + output:
echo "0000:1111:2222:3333:4444:1111:2222:3333:0000" | sed -f sed_cmd 0000:1111:2222:3333:4444:1111:2222:3333:0000 0000;y1111yx:2222:3333:4444x:2222:3333:0000 0000:1111;y2222yx:3333:4444x:3333:0000 0000:1111:2222;y3333yx:4444x:0000 0000:1111:2222:3333:4444FixedFirst Done