【问题标题】:File composition using command line tools (Linux / Mac)使用命令行工具(Linux / Mac)进行文件组合
【发布时间】:2013-09-18 14:46:38
【问题描述】:

我有一个包含一些文本和某种 placeHolder 的文件,以及另一个包含一些其他文本的文件

例如: myText.txt:

some text strings plus a {{myPlaceholderText}} and some more text 

myPlaceholderText.txt:

more text here

我希望能够创建包含字符串的第三个文件:

"some text strings plus a more text here and some more text"

是否可以使用命令行工具来做到这一点?

【问题讨论】:

    标签: html linux bash command


    【解决方案1】:

    我认为 sed 是最简单的方法:

    $ sed "s/{{myPlaceholderText}}/$(<myPlaceholder.txt)/g" myText.txt
    some text strings plus a more text here and some more text 
    

    【讨论】:

      【解决方案2】:

      是的。而 bash 是除了解释型语言之外最安全的常用工具。

      #!/bin/bash
      
      R=$(<myPlaceholderText.txt)
      while read -r LINE; do
          echo "${LINE//'{{myPlaceholderText}}'/$R}"
      done < myText.txt > another_file.txt
      

      输出到 another_file.txt:

      some text strings plus a more text here and some more text
      

      另一个通过awk:

      awk 'BEGIN{getline r < ARGV[1];ARGV[1]=""}{gsub(/{{myPlaceholderText}}/,r)}1' myPlaceholderText.txt myText.txt > another_file.txt
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-31
        • 2012-05-30
        • 2011-12-16
        • 2017-06-07
        • 2014-05-09
        • 1970-01-01
        • 2012-09-28
        相关资源
        最近更新 更多