我最近在这里添加了一个关于如何使用sed和awk在文件中获取文本字符串并使用它来命名文件的问题。现在我正在尝试弄清楚如何反之 - 使用文件名,并在指定点将该文本插入到文件中。
使用相同的文本示例,假设此文件称为test1。嗡嗡声:
begin{question}
A business incurs the following costs per unit: Labor $125/unit; Materials $45/unit and rent $250,000/month. If the firm produces 1,000,000 units a month, the total variable costs equal
begin{answerlist}
item $125Million
item $45Million
item $1Million
item $170Million
end{answerlist}
end{question}
begin{solution}
begin{answerlist}
item F.
item F.
item F.
item F.
end{answerlist}
end{solution}
exname{target}
extype{schoice}
exsolution{0001}
exshuffle{TRUE}
我现在想test1.Rnw
文件名,并将该文本插入到该文件中的 \exname{target} 位置,替换文本target
。最好,我可以在这里只插入test1
而不是.Rnw
扩展名。
我希望我能用我之前的问题(以及非常好的回答(来制定解决方案,但我被困住了。
=======
给定exname{target}
中的target
是你感兴趣的:
1( 根据target
的值命名文件:
awk -F'[{}]' 'NR==FNR{if ($1=="\exname") fname=$2; next} {print > fname}' file file
2( 要将target
替换为当前文件名:
awk -F'[{}]' '$1=="\exname"{ $0 = $1 "{" FILENAME "}" } 1' file
你不需要 shell lop 来操作多个文件,但在这种情况下,它确实有一个简单、吸引人的一致语法:
for file in *; do
awk as above but replace `file` with `"$file"`
done
如果要将原始文件替换为awk命令的输出:
for file in *; do
awk -F'[{}]' 'NR==FNR{if ($1=="\exname") fname=$2; next} {print > fname}' "$file" "$file" &&
rm -f "$file"
done
for file in *; do
awk -F'[{}]' '$1=="\exname"{ $0 = $1 "{" FILENAME "}" } 1' "$file" > tmp &&
mv tmp "$file"
done
您有不同的工作方式,我建议使用sed
,因为您想替换文件中的某些模式。
输入:
$ cat test1.rnw
begin{question}
A business incurs the following costs per unit: Labor $125/unit; Materials $45/unit and rent $250,000/month. If the firm produces 1,000,000 units a month, the total variable costs equal
begin{answerlist}
item $125Million
item $45Million
item $1Million
item $170Million
end{answerlist}
end{question}
begin{solution}
begin{answerlist}
item F.
item F.
item F.
item F.
end{answerlist}
end{solution}
exname{target}
extype{schoice}
exsolution{0001}
exshuffle{TRUE}
命令:
FILENAME=`basename test1.rnw .rnw`; sed 's/^\exname{target}/\exname{'"$FILENAME"'}/' test1.rnw
解释:
basename
命令与文件名和扩展名一起使用以将其从文件名中删除,则结果存储在名为FILENAME
的变量中。- 您可以使用
sed
的find and replace
功能并替换^\exname{target}
(以exname{target}
开头的行exname{
后跟变量的内容FILENAME
并以}
结尾
输出:
begin{question}
A business incurs the following costs per unit: Labor $125/unit; Materials $45/unit and rent $250,000/month. If the firm produces 1,000,000 units a month, the total variable costs equal
begin{answerlist}
item $125Million
item $45Million
item $1Million
item $170Million
end{answerlist}
end{question}
begin{solution}
begin{answerlist}
item F.
item F.
item F.
item F.
end{answerlist}
end{solution}
exname{test1}
extype{schoice}
exsolution{0001}
exshuffle{TRUE}
待办事项:
- 将标准输出重定向到文件以保存结果
- 或使用
-i
或-i.bak
就地进行更改(之前进行备份(
.DOC:
https://www.gnu.org/software/sed/manual/sed.html
如果您是awk
爱好者,您还可以使用以下命令来达到相同的结果:
awk '/^\exname/{noext=FILENAME;sub(/..*$/,"",noext);print "\exname{"noext"}";next}1{print}' test1.rnw
解释:
1{print}
将打印每一行/^\exname/{noext=FILENAME;sub(/..*$/,"",noext);print "\exname{"noext"}";next}
对于以exname
开头的行,则访问 FILENAME 并使用noext=FILENAME;sub(/..*$/,"",noext)
删除扩展名,然后使用print "\exname{"noext"}"
打印该行并转到next
行以避免重复打印。