简体   繁体   中英

Replacing Multiple Lines in Vi Editor

This is some lines of text in a file. I need to remove certain block of text using vi editor.

An aurora (plural: auroras or aurorae) is a natural light display in the sky particularly in the high latitude (Arctic and Antarctic) regions, caused by the collision of energetic charged particles with atoms in the high altitude atmosphere (thermosphere).

Most aurorae occur in a band known as the auroral zone[2][2] which is typically 3° to 6° in latitudinal extent and at all local times or longitudes.

The auroral zone is typically 10° to 20° from the magnetic pole defined by the axis of the Earth's magnetic dipole. During a geomagnetic storm, the auroral zone will expand to lower latitudes. The diffuse aurora is a featureless glow in the sky which may not be visible to the naked eye even on a dark night and defines the extent of the auroral zone.

I have a input file like above. In this file I have to remove certain occurrences of a block of text like the following.

Most aurorae occur in a band known as the auroral zone[2][2] which is typically 3° to 6° in latitudinal extent and at all local times or longitudes.

So, I am using following command which is not working :

:g/^Most/,/auroral/,/longitudes./d

I am deleting the lines starting with Most , auroral in the mid and longitudes at the end.

There are distinct limits on what you can do, but in the context, you could use:

:g/^while/.,/^}/d

to delete a while loop where the while is at the start of a line up to the close brace at the start of a line.


Can you please add some more to this?

The :g/^while/ part searches globally for lines that start with while . What follows is an ex command that is executed for each matched line. The command is .,/^}/d , which means from the current line ( . ) to the next line starting with close brace ( /^}/ ) do a delete ( d ). You can use things like backward searches or relative motions ( ?^{? or .-3 or .+10 ) as well.

It is difficult to tell from the mangled appearance of the comment exactly what you have in mind (not your fault - comments don't preserve useful formatting.)

--------------
Stack Over Flow
c**p
c**p
c**p
cool
c**p
c**p
c**p
------------

I need to replace from Stack Over Flow to ------------ [...]

That is simple, and I don't see how the cool has any effect on it:

:g/^Stack Over Flow/.,/^-------/d

This is isomorphic with my original answer.

It would be easier to delete the block { ... } instead of searching and replacing.

You could go to the block (inside or at the openening bracked) and delete it with daB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM