温馨提示:本文翻译自stackoverflow.com,查看原文请点击:awk - Using gawk to Replace a Pattern of Text with the Contents of a File Whose Filename is Inside the Tex
awk sed text gawk ed

awk - 使用gawk用文件名在Tex内的文件内容替换文本模式

发布于 2020-04-11 14:36:10

我正在尝试根据某些条件替换文本文件中的文本。

例如,如果我有三个文本文件,其中external.txt包含:

   Blah Blah Blah
   INCLUDE inner1.txt
   Etcetera Etcetera
   INCLUDE inner2.txt
   end of file

而inner1.txt包含:

  contents of inner1

而inner2.txt包含:

  contents of inner2

替换结束时,outer.txt文件如下所示:

    Blah Blah Blah
    contents of inner1
    Etcetera Etcetera
    contents of inner2
    end of file

总体模式是,对于单词“ INCLUDE”的每个实例,用该文件的内容替换整行,该文件的文件名紧跟在“ INCLUDE”的实例之后,在一种情况下为inner1.txt,在第二种情况下为大小写为inner2.txt。

简而言之,gawk是否可以根据要在外部文本文件中替换的内容来确定将哪个文本文件嵌入到外部文本文件中?

查看更多

提问者
gawk_Nube
被浏览
76
Cyrus 2018-05-10 03:39

使用GNU awk:

awk --load readfile '{if ($1=="INCLUDE") {printf readfile($2)} else print}' outer.txt