Get substring of a string under UNIX/Linux
there many ways to do so.
str="hello world"
1) cut
Where -cN-M tells the cut command to return columns N to M, inclusive. cut -cN-M
end=5
sub_str=`echo ${str} | cut -c${start}-${end}`
output: hello
2) bash
Note: if you are not sure of having
bash
, consider using cut
. ${str:offset}
${str:offset:length}
sub_str=${str:0:5}
output: hello
3) expr
expr substr string position length
sub_str=`expr substr $str 1 5`
output: hello
posted on 2012-04-01 15:38 gembin 閱讀(795) 評論(0) 編輯 收藏 所屬分類: Linux