Tuesday, April 10, 2012

Search a pattern and provide a context (few lines above and below)

Hello everyone!

I am trying to find a command to achieve a search with a context. Let'
say I have a file containing following lines:

1. dog
2. cat // <- I want to keep this line and one line
above and one line below
3. mouse
4. cow
5. horse
6. dolphin
7. whale
8. cat // <- I want to keep this line and one line above
and one line below
9. sheep
10. bat


I would like to find all the lines containing "cat" and preserve a
line above and below. The rest rest of lines should be deleted. So,
when I run the command my output should look like following:

1. dog
2. cat
3. mouse
7. whale
8. cat
9. sheep


I believe the following command does something similar to what I need

:g/192.168.4.2:5120 ->/z#.3

The only problem is that unwanted lines are not deleted and I can only
browse the file but not save/copy the result.

Anyone knows the answer?

Thanks in advance!

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: