Friday, 19 February 2010

Vi delete line if search does not exist

Right, if you have ever had to scroll through an endless text file looking for the needle in a haystack, there is a simpler way using the vi editor. Using vi, You can use a simple search and delete command to search the file and delete lines where a phrase doesn;t exist, like so;

:g!/text to look for/d

The example above searches all lines for text containing 'text to look for' and deletes the line if the text does not exist.

You could do the opposit and delete lines where the text exists like so;

:g/text to look for/d

No comments: