我正在使用OSX,我试图在.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}
我想抓取\ exname {}字段中的文本,并将其设为文件的新标题。因此,我希望此处的文件包含已发布的内容,整个文件应命名为" target.Rnw"。
下面的链接让我开始了,但我无法使其工作,以便脚本获取基本文件,然后根据\ exname {}字段中的内容重命名它。
https://unix.stackexchange.com/questions/158447/grep-search-expression-and-rename-file
答案 0 :(得分:2)
看看是否有效:
$ cat ip.Rnw
\end{solution}
\exname{target}
\extype{schoice}
\exsolution{0001}
\exshuffle{TRUE}
$ # use { or } as delimiters
$ # print second field if first field is \exname
$ awk -F'[{}]' '$1=="\\exname"{print $2}' ip.Rnw
target
$ # with sed, use capture group and backreference
$ sed -n 's/^\\exname{\(.*\)}/\1/p' ip.Rnw
target
$ # echo is for testing purpose, remove it once things seem fine
$ echo mv ip.Rnw "$(awk -F'[{}]' '$1=="\\exname"{print $2 ".Rnw"}' ip.Rnw)"
mv ip.Rnw target.Rnw