Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a String in CSH?

Tags:

csh

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

like image 744
twimo Avatar asked Oct 12 '11 04:10

twimo


People also ask

How split a string with awk?

The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep. Variable hms is an array so hms[2] is 34 . The last three statements are equivalent, but the last two more convenient for longer arrays. In the second you can specify the start index and number of elements to print.


1 Answers

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.

like image 173
reflux Avatar answered Oct 08 '22 04:10

reflux