How do I split a String in CSH?

csh
twimo picture twimo · Oct 12, 2011 · Viewed 36.9k times · Source

For example, I want to split "one,two,three" with comma as delimiter and use a loop to process the resulted three substring separately.

Answer

reflux picture reflux · Mar 2, 2014

A simpler solution than the current one presented involves using the built-in substitution modifer -- there is no need or reason to wastefully use a loop or external command substitution in this instance:

set list = one,two,three
set split = ($list:as/,/ /)

echo $split[2] # returns two

() creates a list, the :s is the substitution modifier and :as repeats the subtitution as many times as needed.

Furthermore, t/csh does not require quoting of bare strings, nor variables that do not require forced evaluation.