I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-".
I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after the actual diff.
I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-".
I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after the actual diff.
In VIM:
:g!/^[+-]/d
Here is the English translation:
g
lobally do something to all lines that do NOT!
match the regular expression: start of line^
followed by either +
or -
, and that something to do is to d
elete those lines.
diff -u <some args here> | grep '^[+-]'
Or you could just not produce the extra lines at all:
diff --unified=0 <some args>
cat your_diff_file | sed '/^[+-]/!D'
egrep "^[+-]" difffile >outputfile
Instead of deleting everything that doesn't match you show only lines that match. :)
If you need to do something more complex in terms of regular expressions, you should use this site: http://txt2re.com/
it also provides code examples for many different languages.
%!grep -Ev '^[+-]'
does it inline on current file, and can be considerably faster than :v
for large files.
Tested on Vim 7.4, Ubuntu 14.04, 1M line log file.
Lines that don't contain word: https://superuser.com/questions/265085/vim-delete-all-lines-that-do-not-contain-a-certain-word/1187212#1187212