Monday, October 3, 2011

RE: How to match lines containing patternA but not containing patternB?

>From: vim_use@googlegroups.com [mailto:vim_use@googlegroups.com] On Behalf Of superfish
>Sent: Tuesday, 4 October 2011 05:07
>To: vim_use@googlegroups.com
>Subject: How to match lines containing patternA but not containing patternB?
>
>Facing with a data file which contains more than 7 million lines, I need to delete all >lines containing <patternA> but not containing <patternB>.
>So far the only way I can think of is kind messy, something like this:

>:g/<patternB>/s/<patternA>/<patternC>/
>:g/<patternA>/d
>:%s/<patternC>/<patternA>/

>But I really like to know how this can be done in a better way, like a single global >replacement command or something.
 
That seems to cover it, but you can sometimes use a screwdriver instead of pliers: faster, too. If you have Linux or Cygwin GNU tools, you can emulate what you did above, and perhaps put them into a bash script for later use. The bash command using grep & sed is, all on one line:

sed -e /patternB/s/patternA/patternC/ <input.txt | grep -v patternA | sed -e s/patternC/patternA/ >output.txt


Think green - keep it on the screen.

This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.


--
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:

Post a Comment