eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
In a shell script, I want to remove all duplicate elements from various PATH-like environment variables, preserving the original order of the elements. Here is what I have so far:
#!/bin/sh MYPATH="/abc/def:fred:bill g:jock:bill g:/abc/def:/abc/def" echo "MYPATH ='$MYPATH'" MYPATH=`perl -e'my%s;print join":",grep(!$s{$_}++,split(/:/,shift))' " +$MYPATH"` echo "MYPATH2='$MYPATH'"
Though I'm fairly happy with that solution, if I've blundered or you can see a better Perl (or non-Perl) way to do it, please respond away. Because my Unix has become a bit rusty, I'm concerned I may be overlooking a "standard" Unix way to do this. (A quick google uncovered this awk path cleaner but I didn't find a standard Unix command for this).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl one-liner to remove duplicate entries from PATH
by parv (Parson) on Jan 12, 2006 at 06:41 UTC | |
|
Re: Perl one-liner to remove duplicate entries from PATH
by blazar (Canon) on Jan 12, 2006 at 10:43 UTC | |
by sfink (Deacon) on Jan 12, 2006 at 18:09 UTC | |
|
Re: Perl one-liner to remove duplicate entries from PATH
by sh1tn (Priest) on Jan 12, 2006 at 07:37 UTC | |
by muntfish (Chaplain) on Jan 12, 2006 at 12:21 UTC | |
by demerphq (Chancellor) on Jan 12, 2006 at 18:57 UTC |