次のパターンを使用して文字列varを分割します。
TestString="Artical Title Here (ClassYear) [Author]"
次のようにcutを使ってタイトルをVarに入れることができました。
ATH="$(cut -d'(' -f1 <<<$TestString)"
echo $ATH
ただし、角かっこを含む(ClassYear)を別の文字列変数に入れ、角かっこを含む[Author]と同じ方法を見つけることができません。これらのフィールドと型を他の2つの変数にどのように配置できますか?
ベストアンサー1
パラメータを使用した拡張bash
:
# remove matching suffix ` (*`
ath="${TestString%% (*}"
# remove matching prefix `*) `
author="${TestString##*) }"
# remove matching prefix `*(`
classyear="${TestString##*(}"
# remove matching suffix ` [*` and add the missing `(`
classyear="(${classyear%% [*}"