我有一个包含一些文本和某种placeHolder的文件,另一个包含其他文本的文件
例如: myText.txt:
some text strings plus a {{myPlaceholderText}} and some more text
myPlaceholderText.txt:
more text here
我希望能够创建包含字符串的第3个文件:
"some text strings plus a more text here and some more text"
是否可以使用命令行工具执行此操作?
答案 0 :(得分: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
答案 1 :(得分:1)
我认为sed是最简单的方法:
$ sed "s/{{myPlaceholderText}}/$(<myPlaceholder.txt)/g" myText.txt
some text strings plus a more text here and some more text