Shell Golf

Robrt on 2003-08-04T23:12:35

I played shell golf with myself today. (I won.)

I started with
echo $PATH | perl -F: -lane'$,="\n"; print @F'
and Ask intervened with
perl -e'print join"\n",split(/:/,$ENV{PATH}),""'
claiming it was "the same length and is using more common idioms.". I noted that his was a lot more typing and punctuation, and mine is mostly whitespace.

I shifted into Awk for a few cycles:
echo $PATH | awk 'BEGIN{RS=":"} {print}'
echo $PATH | awk -vRS=: '{print}'

but what I really wanted was sed. I settled with perl
echo $PATH | perl -pe's/:/\n/g'
after determining that tcsh was breaking sed.

But..
echo $PATH | tr : '\n'
I win.


This is fun

oneiron on 2003-08-05T09:27:11

My nick is oneiron, so I better take a swing at this. What is the shortest version that uses perl?

perl -le"s,,$PATH,,y,:,\n,,print"
echo $PATH|perl -pey,:,\\n,

Purely shell solution

runrig on 2003-08-05T17:08:34

But you said this was shell golf :-) Though you didn't say what shell. So here's my purely ksh solution:
( IFS=:; for x in $PATH; do print $x; done )

Re:Purely shell solution

Robrt on 2003-08-05T18:08:57

I like this one.

Re:Purely shell solution

prakash on 2003-08-05T18:48:37

As long as we are golfing, why use print when echo is there to save you one char?

( IFS=:; for x in $PATH; do echo $x; done )

/prakash