如何將原始檔案中的文字附加到目標檔案中的 EOF 之前

如何將原始檔案中的文字附加到目標檔案中的 EOF 之前

我有2個文件。檔案 1:來源.sh 檔案 2:目標.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

如果該}字元是最後一行中唯一的字符,且這是唯一隻有一個 的行},則可以將 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

也試試看 - 假設你的 shell 提供了「進程替換」 -

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

相關內容