I’ve got a text-file, and I want to delete all
of the lines which do not contain the sub-string “
Some regex-engines will allow the search-replace
with empty string.
Most of “gnu-based” engines like grep and sed will support that.
For those who won’t,
you can mimic it with a positive, two-steps replacement,
it will require match-memory/backtrack/back-trace support
\1 is commonly used to remember the first match).
I’m using my modded-version of Notepad2, which supports a such search-replace.
first replacement is to “mark a good line”:
#\1 (adds a
# characters at line-start).
second replacement will remove bad-lines with (simple) single-character-NOT rule:
^[^#].*$ with an empty-string (removes lines that do not start with
(and you may probably want to restore the original-line by removing the
# prefix using last search-replace: replace
^# to empty-string)
Also, it is quite flexible,
say I now want to keep lines with “
enum” in-them too,
the first stage is:
#\1 (both cases will the
and the second stage is the same:
^[^#].*$ lines with an empty-string
(again you probably want to remove the
# prefix with replacing
^# with empty-string)
If you think a single-
#-prefix as a “mark” is “too simple”,
you can use
### prefix instead and change the second-stage with replacing
^[^#][^#][^#].*$ with empty-line.
It is so it will be uncommon-enough to avoid false-positive replacements,
so if you’re lines are taken from BASH, shell-scripts, htaccess or APACHE ini-files,
# is a common-character (and often used as a comment-prefix),
choose another character,
if you just use it in your editor, a Hebrew character such as
א will be OK too! :]
The replacement by marking a content using an uncommon phrase,
is a widely-used technique in computer-science (formal-languages)
where “the universe” is a languages, defined by a range of
possible characters (referred to as the “languages ABC”).