How do I split a String in CSH?

扶醉桌前 提交于 2019-12-08 17:11:53

问题


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


回答1:


For example:

set s = "one,two,three"
set words = `echo $s:q | sed 's/,/ /g'`
foreach word ($words:q)
    echo $word:q
end

But consider whether csh is the right tool for whatever job you're doing:

http://www.bmsc.washington.edu/people/merritt/text/cshbad.txt




回答2:


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.




回答3:


set list = one,two,three
foreach i ( $list:as/,/ / )
  echo $i
end


来源:https://stackoverflow.com/questions/7735160/how-do-i-split-a-string-in-csh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!