Skip to main content
deleted 6 characters in body
Source Link
dogbane
  • 30.8k
  • 17
  • 85
  • 61

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];printf s$i;s=":"}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin 

(Updated to remove the trailing semicolon:.)

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];printf s$i;s=":"}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin 

(Updated to remove the trailing semicolon.)

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];printf s$i;s=":"}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin 

(Updated to remove the trailing :.)

removed trailing semicolon
Source Link
dogbane
  • 30.8k
  • 17
  • 85
  • 61

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: -v ORS=: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];printarr[$i];printf $i;s$i;s=":"}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin: 

(Updated to remove the trailing semicolon.)

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: -v ORS=: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];print $i;}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin: 

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];printf s$i;s=":"}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin 

(Updated to remove the trailing semicolon.)

Source Link
dogbane
  • 30.8k
  • 17
  • 85
  • 61

Use awk to split the path on :, then loop over each field and store it in an array. If you come across a field which is already in the array, that means you have seen it before, so don't print it.

Here is an example:

$ MYPATH=.:/foo/bar/bin:/usr/bin:/foo/bar/bin $ awk -F: -v ORS=: '{for(i=1;i<=NF;i++) if(!($i in arr)){arr[$i];print $i;}}' <<< "$MYPATH" .:/foo/bar/bin:/usr/bin: