如何将源文件中的文本附加到目标文件中的 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

相关内容