Skip to main content
added 2 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< $PATH`"$PATH"` 

Explanation of awk code:

  1. Separate the input by colons.
  2. Append new path entries to associative array for fast duplicate look-up.
  3. Prints the associative array.

In addition to being terse, this one-liner is fast: awk uses a chaining hash-table to achieve amortized O(1) performance.

based on Removing duplicate $PATH entries

PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< $PATH` 

Explanation of awk code:

  1. Separate the input by colons.
  2. Append new path entries to associative array for fast duplicate look-up.
  3. Prints the associative array.

In addition to being terse, this one-liner is fast: awk uses a chaining hash-table to achieve amortized O(1) performance.

based on Removing duplicate $PATH entries

PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< "$PATH"` 

Explanation of awk code:

  1. Separate the input by colons.
  2. Append new path entries to associative array for fast duplicate look-up.
  3. Prints the associative array.

In addition to being terse, this one-liner is fast: awk uses a chaining hash-table to achieve amortized O(1) performance.

based on Removing duplicate $PATH entries

Source Link
Leftium
  • 173
  • 1
  • 8

PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< $PATH` 

Explanation of awk code:

  1. Separate the input by colons.
  2. Append new path entries to associative array for fast duplicate look-up.
  3. Prints the associative array.

In addition to being terse, this one-liner is fast: awk uses a chaining hash-table to achieve amortized O(1) performance.

based on Removing duplicate $PATH entries