Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing and Printing $PATH Using Unix

I've placed my PATH in a text file and would like to print each path on a newline using a simple command in UNIX.

I've found a long way to do it that goes like this...

cat Path.txt | awk -F\; '{print $1"\n", $2"\n", ... }'

This however seems inefficient so I know there must be a way to quickly print out my results on new lines each time without having to manually call each field separated by the delimiter.

like image 447
Nick Avatar asked Oct 23 '12 23:10

Nick


2 Answers

Yet another way:

echo $PATH | tr : '\n'

or:

tr : '\n' <Path.txt
like image 102
Olaf Dietsche Avatar answered Sep 25 '22 12:09

Olaf Dietsche


The tr solution is the right one but if you were going to use awk then there'd be no need for a loop:

$ echo "$PATH"
/usr/local/bin:/usr/bin:/cygdrive/c/winnt/system32:/cygdrive/c/winnt

$ echo "$PATH" | awk -F: -v OFS="\n" '$1=$1'
/usr/local/bin
/usr/bin
/cygdrive/c/winnt/system32
/cygdrive/c/winnt
like image 25
Ed Morton Avatar answered Sep 24 '22 12:09

Ed Morton