複数の文字列を異なるマッピングされた文字列セットに置き換える

複数の文字列を異なるマッピングされた文字列セットに置き換える

複数の文字列パターンを他の事前定義された文字列に置き換えたいと思います。

たとえば、

入力する:

This sentence will be converted to something different. This sentence can contain same words.

出力:

These words need to be replaced with something different. These words can contain same alphabets. 

だからここでは次のパターンに変換したいと思います。

  • これら => これら
  • 文=>単語
  • =>必要
  • 変換=>置換
  • =>〜で
  • 単語=>アルファベット

できれば教えてください。

ベストアンサー1

data.txtたとえば、データがファイルにある場合は、sedforループ内で使用できます。たぶん、次のようなものがあります。

replacements=(
    This:These
    sentence:word
    will:need to
    converted:repalced
    to:with
    words:alphabets
)

for row in "${replacements[@]}"; do
    original="$(echo $row | cut -d: -f1)";
    new="$(echo $row | cut -d: -f2)";
    sed -i -e "s/${original}/${new}/g" data.txt;
done

おすすめ記事