区切り文字の最初の出現に文字列を分割する

区切り文字の最初の出現に文字列を分割する

次の形式の文字列があります

id;some text here with possible ; inside

の最初の発生でこれを2つの文字列に分割しようとしています;。したがって、次のようにidする必要があります。some text here with possible ; inside

文字列を分割する方法(使用)を知っていますが、左側の部分cut -d ';' -f1にあるため、より多くの部分に分割されます。;

ベストアンサー1

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

おすすめ記事