#includeをエミュレートする方法Linuxコマンドの使用

#includeをエミュレートする方法Linuxコマンドの使用

그래서 다른 파일이 포함된 소스 파일에서 출력 파일을 만들려고 합니다.

(내 사용 사례는 실제로 Kubernetes/OpenShift용 YAML이지만 이 .txt 파일은 목표를 보여줍니다.)

예를 들어:

% cat source.txt
This is the source files it will include two other files.
The first will be here:
#INCLUDE ./first-included-file.txt
And the second file will be inserted here:
#INCLUDE ./second-included-file.txt
And this is the end of source.txt

포함된 파일이 다음과 같은 경우:

% cat first-included-file.txt
This is FIRST
End of FIRST

% cat second-included-file.txt
This is SECOND
End of SECOND

그러면 출력은 다음과 같습니다.

This is the source files it will include two other files.
The first will be here:
This is FIRST
End of FIRST
And the second file will be inserted here:
This is SECOND
End of SECOND
And this is the end of source.txt

다른 답변 사용

sed '/#INCLUDE/ r insertfile'

그러나 소스의 값에서 파일 이름을 찾는 일반적인 솔루션이 있습니까?

각 줄을 읽고 구문 분석하는 Bash 스크립트가 작업을 수행할 수 있을 것 같지만 아마도 awk다른 것이 이 작업을 수행할 수 있을까요?

ベストアンサー1

프로그램 파일- C 전처리기 명령을 사용할 수 있습니다. 이 명령은 일반적으로 대부분의 Linux 배포판 gcc이나 소프트웨어 패키지에 포함되어 있습니다.cpp

C 전처리기라고 부르지만 다른 파일에도 사용할 수 있고, 등 의 표준 C 전처리기 지시문을 사용할 수 있습니다 #include.#define#ifdef

예를 들어:

소스파일.txt

This is the source files it will include two other files.
The first will be here:
#include "first-included-file.txt"
And the second file will be inserted here:
#include "second-included-file.txt"
And this is the end of source.txt

첫 번째 포함 file.txt

This is FIRST
End of FIRST

두 번째 포함 file.txt

This is SECOND
End of SECOND

산출cpp -P source.txt

$ cpp -P source.txt
This is the source files it will include two other files.
The first will be here:
This is FIRST
End of FIRST
And the second file will be inserted here:
This is SECOND
End of SECOND
And this is the end of source.txt

노트:

  • -P플래그는 전처리기의 출력에서 ​​라인 마커 생성을 억제합니다.
  • 매뉴얼 페이지
  • "#include, #define, #ifdef 등과 같은 표준 C 전처리기 지시문을 사용할 수 있습니다." - 그러나 이를 원하지 않고 파일을 있는 그대로 포함하려는 경우 각 #INCLUDE를 #include로 변환하고 포함된 파일 이름을 인용하는 것 외에도 문자를 비활성화하는 작업을 수행해야 합니다. cpp를 실행하기 직전에 #include, #define, #ifdef 등의 문자열이 입력에 나타날 수 있습니다. 그렇지 않으면 이러한 문자열의 발생에 따라 텍스트에 불필요한 변환이 수행됩니다.
  • 포함할 파일이 로컬 디렉터리에 없는 경우(cpp 구현이 일반적으로 로컬 디렉터리를 먼저 검색한다고 가정할 때) 예기치 않은 결과가 나타날 수도 있지만 cpp는 다음 중 하나에서 동일한 이름을 가진 파일을 찾을 수 있습니다. 다음 디렉터리: it 파일이 포함된 다른 구현 정의 디렉터리를 검색합니다.

おすすめ記事