Thursday, June 10, 2010

Different ways to split the PATH variable

  Hardly a day passes for a UNIX programmer or an administrator without executing the command 'echo $PATH' . A typical output of this command will be as shown below:
#echo $PATH
  In the above example, for simplicity, we have shown only 4 components in the PATH variable. However, in a real-time environment, the number of components in a PATH variable could be really lot. And hence it becomes really cumbersome to interpret OR to know whether a particular component in the PATH variable what we are looking for is present or not. In fact, even doing grep on the 'echo $PATH' wont help us since either it will return the whole thing back or nothing.

  It would have been more readable had the PATH variable been split or broken and displayed as shown below:
Let us see the different ways how we can split the PATH variable:

1. The simplest and my favorite is echo piped with tr. (tr is one of the most powerful commands ). This simply substitutes ':' with a new line.
#echo $PATH | tr ':' '\n'
2. There are many ways in awk to achieve the same. This one is using the record separator RS.
#echo $PATH | awk  '1' RS=":"
     The default RS is a new line. In this case, we tell awk to consider ':' as the record separator, and hence it prints every component on encountering the ':'.

3. awk provides a gsub function using which the we can split the variable. The gsub substitutes all ':' to newline:
#echo $PATH | awk 'gsub ( ":","\n" )'
4. Using the ':' as delimiter, we can read the different components as different columns:
#echo $PATH | awk -F: '{ for(j=1;j<=NF;j++) print $j;}'
5. The last one using the 'sed'. However, this is the least, I would prefer among the rest. sed replaces('s') all(g) occurrences of  ':' with a form-feed character. The newline cannot be used as a replacement in sed as with 'tr' command and hence this workaround:
#echo $PATH | sed "s/:/`echo  '\f'`/g"
   Friends, in every article, we try to solve a problem with as many different options as possible. The reason is simple: Next time we encounter an issue, we start thinking in many different ways.

Happy Unix Programming!!!

1 comment:

  1. Regarding the last option using sed, the regular way of substituting

    echo $PATH | sed 's/:/\n/g'

    does work in Redhat Linux, but does not in some other flavors of UNIX.