我有以下格式的字符串
id;some text here with possible ; inside
并希望在第一次出现时将其拆分为2个字符串;
。因此,应该是:id
和some text here with possible ; inside
我知道如何分割字符串(例如,使用cut -d ';' -f1
),但是由于我;
位于左侧,因此它将分割为更多部分。
cut
听起来像是一个合适的工具:
bash-4.2$ s='id;some text here with possible ; inside'
bash-4.2$ id="$( cut -d ';' -f 1 <<< "$s" )"; echo "$id"
id
bash-4.2$ string="$( cut -d ';' -f 2- <<< "$s" )"; echo "$string"
some text here with possible ; inside
但是read
更合适:
bash-4.2$ IFS=';' read -r id string <<< "$s"
bash-4.2$ echo "$id"
id
bash-4.2$ echo "$string"
some text here with possible ; inside
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句