[英]How do I insert a line above specific lines in a file using Vim or Perl?
I'd like to insert the line 我想插入这一行
<hr />
above every occurrence of a header 2 line in a file - eg, above this pattern 在文件中每次出现标题2行时 - 例如,在该模式之上
<h2>variable pattern here</h2>
So the above should become 所以上面应该成为
<hr />
<h2>variable pattern here</h2>
How can I do this with Vim, Sed or Perl? 我怎么能用Vim,Sed或Perl做到这一点?
vim way: vim方式:
cmd :g/<h2>/normal O<hr />
will do the job. cmd
:g/<h2>/normal O<hr />
将完成这项工作。
see it here: (I took the example from sudo_O) 在这里看到:(我从sudo_O拿了例子)
With sed
you could do sed '/<h2>/i <hr />'
: 使用
sed
你可以做sed '/<h2>/i <hr />'
:
$ cat file
<html>
<h2>variable pattern here</h2>
<h3>not here</h3>
<h2>heading</h2>
<h2>Something</h2>
$ sed '/<h2>/i <hr />' file
<html>
<hr />
<h2>variable pattern here</h2>
<h3>not here</h3>
<hr />
<h2>heading</h2>
<hr />
<h2>Something</h2>
The first part /<h2>/
matches line containing <h2>
and the second part uses the i
command to insert <hr />
above the matched line. 第一部分
/<h2>/
匹配包含<h2>
,第二部分使用i
命令在匹配的行上方插入<hr />
。
A nice option with sed
is -i
this save the changes back to the file instead of printing to stdout
but be sure that the changes are correct first. sed
一个不错的选择是-i
这将更改保存回文件而不是打印到stdout
但要确保更改是正确的。
sed -i '/<h2>/i <hr />' file
One of the many ways to do that in Vim: 在Vim中做到这一点的众多方法之一:
:g/h2/norm O<hr /><CR>
Breakdown: 分解:
:g[lobal]
acts on every line that match a pattern, see :h :global
. :g[lobal]
作用于与模式匹配的每一行,请参阅:h :global
。
h2
the pattern we are looking for, it could be made a bit smarter, probably. h2
我们正在寻找的模式,它可能会变得更聪明一些。
norm[al]
runs a normal mode command, see :h :normal
. norm[al]
运行正常模式命令,请参阅:h :normal
。
O
opens a new line above the current line and enters insert mode. O
在当前行上方打开一个新行并进入插入模式。
<hr />
is what you want to insert. <hr />
是您要插入的内容。
We hit <CR>
( <RETURN>
) to run the whole thing. 我们点击
<CR>
( <RETURN>
)来运行整个事情。
Another way, using a single substitution: 另一种方法,使用单一替换:
:%s/^\s*<h2/<hr \/>\r&<CR>
Breakdown: 分解:
:%s[ubstitute]/
performs the substitution on every line of the buffer, see :h :s
. :%s[ubstitute]/
在缓冲区的每一行上执行替换,请参阅:h :s
。
^
anchors the pattern to the beginning of the line. ^
将模式锚定到行的开头。
\\s*
matches any number (0 to many) of white space characters. \\s*
匹配任何数字(0到多个)空格字符。 It is not strictly needed if you are sure that all your HTML tags are on column 1. 如果您确定所有HTML标记都在第1列,则不是严格要求的。
<h2
is the pattern we are really looking for. <h2
是我们真正寻找的模式。
<hr />
is what we want to insert. <hr />
是我们想要插入的内容。
Since we want it to be on its own line, it is followed by a \\r
, 因为我们希望它在自己的行上,所以后面跟着一个
\\r
,
and, finally, the matched text, &
. 最后,匹配的文本
&
。
use strict;
use warnings;
use Tie::File;
tie my @file, 'example.html'
or die "Unable to tie file: $!";
@file = map { m!<h2>.*</h2>!
? ( "<hr />", $_ )
: $_ } @file;
untie @file;
Command line solution in Perl. Perl中的命令行解决方案。
perl -i~ -p -e'/<h2>/ and $_ = "<hr />\n$_"' your_file.html
Explanation of command line flags: 命令行标志的说明:
If the line contains ( /<h2>/
) then prepend <hr />
to it (the current line is in $_). 如果该行包含(
/<h2>/
),则将<hr />
到它(当前行在$ _中)。
But have you considered if this is the best approach? 但你有没有考虑过这是最好的方法? If you want to add a line above every H2 element, then perhaps you should do that with CSS?
如果你想在每个H2元素上面添加一行,那么也许你应该用CSS做到这一点?
perl -plne 'print "<hr />" if(/\<h2\>variable pattern here\<\/h2\>/)' your_file
Input file: 输入文件:
> cat temp
<h2>variable pattern here</h2>
1
<h2>variable pattern here</h2>
2
<h2>variable pattern here</h2>
3
4
<h2>variable pattern here</h2>
Now the execution 现在执行
> perl -plne 'print "<hr />" if(/\<h2\>variable pattern here\<\/h2\>/)' temp
<hr />
<h2>variable pattern here</h2>
1
<hr />
<h2>variable pattern here</h2>
2
<hr />
<h2>variable pattern here</h2>
3
4
<hr />
<h2>variable pattern here</h2>
This will just output to the console. 这将只输出到控制台。 If you want to change it inplace:
如果要在适当的位置更改它:
perl -pi -lne 'print "<hr />" if(/\<h2\>variable pattern here\<\/h2\>/)' your_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.