ソースファイルから宛先ファイルの EOF の前にテキストを追加する方法

ソースファイルから宛先ファイルの EOF の前にテキストを追加する方法

ファイルは 2 つあります。ファイル 1: source.sh ファイル 2: destination.sh

ファイル 1 の内容をファイル 2 に挿入したいのですが、ファイルの末尾に内容を挿入したくありません。ファイル 2 の最後の行は "}" です。

ファイル 1 の内容をファイル 2 の "}" の前に挿入したい

私の正確なコード:

xxx_ecmwf_scoring_state_machine_arn = "${aws_sfn_state_machine.xxx_ecmwf_main_state_machine.id}"

 xxx_ecmwf_etl_state_machine_arn = "${aws_sfn_state_machine.xxx_ecmwf_etl_state_machine.id}"

 xxx_ecmwf_scoring_function_name = "${aws_lambda_function.invoke_xxx_ecmwf_scoring_state_machine_lambda.function_name}"

私の目的地.sh

{
    zzz_ecmwf_scoring_state_machine_arn = "${aws_sfn_state_machine.zzz_ecmwf_main_state_machine.id}"

    zzz_ecmwf_etl_state_machine_arn = "${aws_sfn_state_machine.zzz_ecmwf_etl_state_machine.id}"

    zzz_ecmwf_scoring_function_name = "${aws_lambda_function.invoke_zzz_ecmwf_scoring_state_machine_lambda.function_name}"

    ccc_ecmwf_scoring_state_machine_arn = "${aws_sfn_state_machine.ccc_ecmwf_main_state_machine.id}"

    ccc_ecmwf_etl_state_machine_arn = "${aws_sfn_state_machine.ccc_ecmwf_etl_state_machine.id}"


    ccc_ecmwf_scoring_function_name = "${aws_lambda_function.invoke_ccc_ecmwf_scoring_state_machine_lambda.function_name}"

    rrr-ltf_ecmwf_scoring_state_machine_arn = "${aws_sfn_state_machine.rrr-ltf_ecmwf_main_state_machine.id}"

    rrr-ltf_ecmwf_etl_state_machine_arn = "${aws_sfn_state_machine.rrr-ltf_ecmwf_etl_state_machine.id}"

    rrr-ltf_ecmwf_scoring_function_name = "${aws_lambda_function.invoke_rrr-ltf_ecmwf_scoring_state_machine_lambda.function_name}"

    rrr_ecmwf_scoring_state_machine_arn = "${aws_sfn_state_machine.rrr_ecmwf_main_state_machine.id}"

    rrr_ecmwf_etl_state_machine_arn = "${aws_sfn_state_machine.rrr_ecmwf_etl_state_machine.id}"

    rrr_ecmwf_scoring_function_name = "${aws_lambda_function.invoke_rrr_ecmwf_scoring_state_machine_lambda.function_name}"
    **}**

事前にsource.shをdestination.shに挿入する必要があります}

実稼働中なので、EOFを定義するために数字をハードコードしたくありません... ご協力いただければ幸いです

以下は宛先ファイルのEOFの出力です。

コマンドを実行しました

{ echo "---------"; nl "$filenamelocal" | tail -n 4 ; echo "---------"; }

ここに画像の説明を入力してください

答え1

}文字が最後の行の唯一の文字であり、これが 1 つの のみを含む唯一の行である場合}、File1 を File2 に追加し、その後 を}最後の行に移動することができます。

コードで表すと次のようになります。

# append file1 to file2
cat File1 >> File2
# move the curly brace to the end of the file (change in place (-i flag))
# /^}$/ search for a line which starts with a } and ends afterwards
# d     delete the line
# ;     next command
# $a    append at the last line
# }     curly brace character
sed -i '/^}$/d;$a}' File2

答え2

また、シェルが「プロセス置換」を提供している場合は、以下も試してください。

tac file2 | sed 1r<(tac file1) | tac

関連情報