将选定的文本行末尾字符替换为空格

时间:2009-08-14 10:34:36

标签: regex vim replace

我有文本文件:

    gvim file.txt


In UNIX you can type this at any command prompt.  If you are running Microsoft
Windows, open an MS-DOS prompt window and enter the command.
   In either case, Vim starts editing a file called file.txt.  Because this
is a new file, you get a blank window. This is what your screen will look
like:



    +---------------------------------------+
    |#                                      |
    |~                                      |
    |~                                      |
    |~                                      |
    |~                                      |
    |"file.txt" [New file]                  |
    +---------------------------------------+
          ('#" is the cursor position.)

The tilde (~) lines indicate lines not in the file.  In other words, when Vim
runs out of file to display, it displays tilde lines.  At the bottom of the
screen, a message line indicates the file is named file.txt and shows that you
are creating a new file.  The message information is temporary and other
information overwrites it.

树文件:

^$
text. text\n   - join lines in this block, because ends block dot
more text.\n
^$
^$
text.\n
text: text\n    - join lines in this block, because ends block >
more text>\n
^$
text.\n   - but not this, because don't ends '., :, > or !'
text\n
^$

TASK:

  1. 我想选择一个包含多个行的文本块,其中.:>!仅使用正则表达式
  2. 将行尾字符替换为空格
  3. 如何使用vim,bash,perl或awk来解决此任务。
  4. 我想了解操作的不同之处,选择使用文本的最佳工具。


    尝试在Vim中创建一个正则表达式:

    1. /^\s*\(.*\).

      这将选择所有块文本

    2. /[!\.:>]$

      此选择已结束.:>!

    3. /^\s*\(.*\)[!\.:>]$

      此仅限选择行已结束.:>!

    4. /^\s*\(.*\)[!\.:>]

      此选择仅包含.:>!

    5. 的行

3 个答案:

答案 0 :(得分:0)

选择具有多行结尾的单行:

/^(.*)[!.:>]$/;

如果我尝试使用perl处理块,那么我会一次处理一行并匹配每一行。

答案 1 :(得分:0)

顺便说一句,如果你的主要目的是修复Unix和Windows之间的结束差异,那么值得知道简单的Unix工具dos2unix将转换任意方向的文本文件。

答案 2 :(得分:0)

在vim中,请执行以下操作:

:#s/^(.*)[!.:>]$/ /gc;