0

我有一个包含一些文本和某种 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"

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

4

2 回答 2

1

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

$ sed "s/{{myPlaceholderText}}/$(<myPlaceholder.txt)/g" myText.txt
some text strings plus a more text here and some more text 
于 2013-09-18T15:04:55.923 回答
1

是的。而 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
于 2013-09-18T14:53:56.150 回答