简体   繁体   English

如何使用Vim或Perl在文件中的特定行上方插入一行?

[英]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: 分解:

  1. :g[lobal] acts on every line that match a pattern, see :h :global . :g[lobal]作用于与模式匹配的每一行,请参阅:h :global

  2. h2 the pattern we are looking for, it could be made a bit smarter, probably. h2我们正在寻找的模式,它可能会变得更聪明一些。

  3. norm[al] runs a normal mode command, see :h :normal . norm[al]运行正常模式命令,请参阅:h :normal

  4. O opens a new line above the current line and enters insert mode. O在当前行上方打开一个新行并进入插入模式。

  5. <hr /> is what you want to insert. <hr />是您要插入的内容。

  6. We hit <CR> ( <RETURN> ) to run the whole thing. 我们点击<CR><RETURN> )来运行整个事情。

Another way, using a single substitution: 另一种方法,使用单一替换:

:%s/^\s*<h2/<hr \/>\r&<CR>

Breakdown: 分解:

  1. :%s[ubstitute]/ performs the substitution on every line of the buffer, see :h :s . :%s[ubstitute]/在缓冲区的每一行上执行替换,请参阅:h :s

  2. ^ anchors the pattern to the beginning of the line. ^将模式锚定到行的开头。

  3. \\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列,则不是严格要求的。

  4. <h2 is the pattern we are really looking for. <h2是我们真正寻找的模式。

  5. <hr /> is what we want to insert. <hr />是我们想要插入的内容。

  6. Since we want it to be on its own line, it is followed by a \\r , 因为我们希望它在自己的行上,所以后面跟着一个\\r

  7. 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: 命令行标志的说明:

  • -i In-place editing (replace existing file), back-up to your_file.html~ -i就地编辑(替换现有文件),备份到your_file.html~
  • -p Print each line in the file -p打印文件中的每一行
  • -e Code to execute for each line in the file -e为文件中的每一行执行的代码

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.

相关问题 如何匹配上下两行,然后使用Perl在其中插入单词? - How to match above and below lines, then insert words in between them by using Perl? Perl:如何逐行搜索文本文件以查找特定模式? - Perl: How do I search a text file line by line to find a specific pattern? 使用 python 如何在文本文件的选择行中插入字符串,其中插入的字符串取决于行的内容和已知映射? - Using python how do I insert a string in select lines of a text file where the inserted string depends on the content of the line and a known mapping? Perl:如何在文件中插入行? - Perl:How to insert line in a file? 如何在Perl中的匹配行之后插入两行 - How to insert lines, 2 lines after the matched line in Perl 如何使用 perl 从文件中搜索和提取注释行? - How do i search and extract commented lines from a file using perl? 我如何在Perl中将文件中的行读入哈希 - How do i read lines from a file into a hash in Perl 如何更改保留所有其他行的文件中包含的特定行中的字符串? - How Do I change a string in a specific line contained in a file preserving all other lines? 如何在不使用Perl中的正则表达式的情况下在文件中找到特定行? - How can I find a specific line in a file without using regular expressions in Perl? 如何使用 Perl 正则表达式提取多行代码? - How do I extract multiple lines of code using Perl regex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM