我有一个名为a.tex的.tex文件,其中包含多行文本,如下例所示:

\begin{pycode}

Some text right here, let's say Text 1A: like: There are #cat and #dog.

\end{pycode}

Some text right here, let's say Text 1B: like: One day the #dog tried to run away.


\begin{pycode}

Some text right here, let's say Text 2A: like: There are #cat and #dog and #pig.

\end{pycode}

Some text right here, let's say Text 2B: like: There is #something here.

我想将任何#改为"所提及文本的编号",例如,句子"有#只cat 和#只狗".应该变成"这是一只狗和一只cat ."因为它在文本1a中."有一天,这只#狗试图逃跑."变成了"有一天那只狗试图逃跑.""有#只cat 、#只狗和#只猪."改为"有2只cat 、2只狗和2只猪",依此类推.

输出是一个.tex文件,此更改应用于整个文档.

所以我想要的是:

\begin{pycode}

Some text right here, let's say Text 1A: like: There are 1cat and 1dog.

\end{pycode}

Some text right here, let's say Text 1B: like: One day the 1dog tried to run away.


\begin{pycode}

Some text right here, let's say Text 2A: like: There are 2cat and 2dog and 2pig.

\end{pycode}

Some text right here, let's say Text 2B: like: There is 2something here.

我在这方面没有最低限度的工作.我的 idea 是从第一行开始搜索和替换.例如,如果我们看到"Begin{pycode}"则s=s+1(对于某些计数变量s)并搜索#,然后用s替换它,直到我们遇到下一个"Begin{pycode}".

我正在用这种方式寻找解决方案,但仍然需要时间来找到解决方案.

谢谢你的帮助.

推荐答案

$ awk '/\\begin[{]pycode}/{s++} {gsub(/#/,s); print}' a.tex
\begin{pycode}

Some text right here, let's say Text 1A: like: There are 1cat and 1dog.

\end{pycode}

Some text right here, let's say Text 1B: like: One day the 1dog tried to run away.


\begin{pycode}

Some text right here, let's say Text 2A: like: There are 2cat and 2dog and 2pig.

\end{pycode}

Some text right here, let's say Text 2B: like: There is 2something here.

Linux相关问答推荐

在PowerShell for Linux上,打印出的maven命令还有其他字符

为什么硬编码的阿拉伯字母与Unicode代码点不具有相同的值

boost-iostreams 1.59 sparc-solaris 交叉编译失败

std::chrono::time_zone 在不同操作系统上不可用

如何正确Forking 并完成进程以避免 EAGAIN 错误

Docker 不保留 chown 用户设置

记录终端输入和输出的 Bash 脚本

Docker 默认使用交换文件吗?

如何在具有多种可能性的linux shell中获取最大值和最小值?

获取S3路径的linux命令

如何用 Sed 替换 Match 后的 2 个连续行

Eclipse 的 C# 插件

来自远程主机的 mysqldump

exec 系统调用(如 exec 和 execve)系列的功能有什么区别?

比较linux中两个未排序的列表,列出第二个文件中的唯一性

如果关键字触发然后执行命令,Shell 脚本来监视日志(log)文件?

Linux 上真的没有异步块 I/O 吗?

如何限制我网站的 API 用户?

内联 if shell 脚本

如何制作和应用SVN补丁?