ターゲットファイルのEOFの前にソースファイルのテキストを追加する方法

ターゲットファイルのEOFの前にソースファイルのテキストを追加する方法

2つのファイルがあります。ファイル1:source.shファイル2:target.sh

file1の内容をfile2に挿入したいと思います。しかし、ファイルの最後にコンテンツを挿入したくありません。ファイル2の最後の行は「}」です。

ファイル2の「}」の前にファイル1の内容を挿入したいと思います。

私の正確なコード:

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}"

mydestination.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

おすすめ記事