Sunday, June 17, 2007

Split PATH: AWK digest


This is my PATH env variable set to

$ echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/home/jsaikia/impscripts/

Now to SPLIT the PATH:

$ echo $PATH | awk -F : '{print "PATH is set to"} {for (i=1;i<=NF;i++) {print "["i"]",$i}}'
PATH is set to
[1] /usr/local/bin
[2] /usr/bin
[3] /bin
[4] /usr/bin/X11
[5] /usr/games
[6] /home/jsaikia/impscripts/

Another simple way of doing it:

$ echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/

Make this a function in .bash_profile or .profile.

mypath () {
echo $PATH | tr ':' '\n' | awk '{print "["NR"]"$0}'
}

which will split out the elements of a PATH, one per line.

$ mypath
[1]/usr/local/bin
[2]/usr/bin
[3]/bin
[4]/usr/bin/X11
[5]/usr/games
[6]/home/jsaikia/impscripts/

No comments:

© Jadu Saikia www.UNIXCL.com