如何在shellscript中对字符串进行标记?
例如:给定字符串“a:b:c”和分隔符“:”,我想返回一个数组{a,b,c}
它等同于java的StringTokenizer。
答案 0 :(得分:3)
var="a:b:c"
OLDIFS=$IFS
IFS=":"
read -a array <<< "$(printf "%s" "$var")"
IFS=$OLDIFS
echo ${#array[@]}
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
答案 1 :(得分:0)
echo "a:b:c" | awk -F":" '{print "{"$1",",$2",",$3"}"}'