대상 파일의 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

해당 }문자가 마지막 줄의 유일한 문자이고 하나만 있는 유일한 줄인 경우 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

관련 정보