如何自动化代码的修改?
我有大量的代码,包括数千个方程式。两个结缔线的示例,与第998和999行相对应的示例
sum(x[i]*y[i]for i in 1:n) == 15;
sum(z[i]*x[i]for i in 1:n) == 30;
我想用以下
sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 <= s[998];
sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 <= s[999];
如何自动化此过程来替换此类行?
I have a large piece of code consisting of thousands of equations. Example of two connective lines, corresponding to lines 998 and 999 is
sum(x[i]*y[i]for i in 1:n) == 15;
sum(z[i]*x[i]for i in 1:n) == 30;
I would like to replace such lines with the following
sum(x[i]*y[i]for i in 1:n) - 15 >= -s[998];
sum(x[i]*y[i]for i in 1:n) - 15 <= s[998];
sum(z[i]*x[i]for i in 1:n) - 30 >= -s[999];
sum(z[i]*x[i]for i in 1:n) - 30 <= s[999];
How to automate this process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换
我们的第一步是转换每条线:
向此:
虽然在一个命令中进行操作将是一个好的客厅技巧,但我们将在几个更容易地遵循步骤中进行操作。
==
to-
这是我们的第一个命令,非常简单:
:
输入命令行模式。%
是执行以下命令的行范围。在这里,%
是1,$
(从第1行到最后一行)的快捷方式,因此“每行”。s
是“替代”命令,请参见:help:s
。/==
是您要替换的。/ -
是您要用它代替的。&lt; cr&gt;
(enter)执行命令。简单的英语:“用
-
替换==
”。我们应该得到类似的东西:
;
to&gt; = -s [xxx];
这是我们的第二个命令:
我们将每个
;
替换为通用&gt; = -s [xxx];
。这也很简单。简单英语:“用
;
用&gt; = -s [xxx];
。。我们应该得到类似的东西:
xxx
到行号这是我们的第三个命令:
此命令与其他命令之间的重大变化是,替换部分是动态的。使用
\ =
,我们使用每个执行过程中评估的表达式,而不是固定的字符串。line('。')
是一个返回行号的vimscript函数,这正是我们在这些方括号之间想要的。简单的英语:“用当前行号替换
xxx
。我们应该得到类似的东西:
在这里复制
,我们将每行重复一个命令:
:g
是:help:help:global
命令。/^/
匹配每一行,因此每行将执行以下命令。t
是:help:t
命令。。
表示当前行。|
分开两个命令。s/-s/s
在重复的行上的s
之前,请删除-
。用简单的英语:“标记每条行,然后在自身下方复制每个标记的行,然后在
s
之前删除该领先的-
”。我们应该得到类似的东西:
最后一件事
我们使用最后一个命令在我们的块之间添加一条线:
s [
都标记为标记。:帮助:放置
以附加空行。用简单的英语:“在每行之后,用
s [
>“放一条空行”。我们应该得到类似的东西:
我们学到了什么?
执行简单的替代:help:替换
,的概念:help:range
,匹配特定模式的行上执行任意命令
:帮助子
的地址:help:t
,:help:put
,如何用分开命令的 :help:|
。Substitutions
Our first step is to transform each individual line:
into this:
While doing it in one command would be a nice parlor trick, we will do it in several easier to follow steps.
==
to-
Here is our first command, it is very simple:
:
enters command-line mode.%
is the range of lines on which to execute the following command. Here,%
is a shortcut for1,$
(from line 1 to last line), thus "every line".s
is the "substitute" command, see:help :s
./==
is what you want to substitute./-
is what you want to substitute it with.<CR>
(ENTER) to execute the command.In plain english: "substitute every
==
with-
".We should get something like:
;
to>= -s[XXX];
Here is our second command:
Where we substitute every
;
with a generic>= -s[XXX];
. This is also quite simple.In plain english: "substitute every
;
with>= -s[XXX];
.We should get something like:
XXX
to line numberHere is our third command:
The big change between this command and the other ones is that the replacement part is dynamic. With
\=
, we use an expression that is evaluated during each execution instead of a fixed string.line('.')
is a vimscript function that returns a line number, which is exactly what we want between those brackets.In plain english: "substitute every
XXX
with the current line number".We should get something like:
Duplication
Here we duplicate each line with a single command:
:g
is the:help :global
command./^/
matches every line so the following command will be executed on every line.t
is the:help :t
command..
represents the current line.|
separates two commands.s/-s/ s
removes the-
before thes
on the duplicated line.In plain english: "mark every line, then copy each marked line below itself, then remove that leading
-
before thes
".We should get something like:
One last thing
We use one last command to add a line between our blocks:
s[
is marked.:help :put
to append an empty line.In plain english: "put an empty line after each line with
s[
".We should get something like:
What did we learn?
:help :substitute
,:help :range
,:help sub-replace-\=
,:help :global
,:help :t
,:help :put
,:help :|
.