39

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.

mager
  • 4,813
  • 8
  • 29
  • 30

7 Answers7

73

In VIM:

:g!/^[+-]/d

Here is the English translation:

globally 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 delete those lines.

Marcin
  • 12,245
  • 9
  • 42
  • 49
2

sed -e '/^[^+-]/d'

ennuikiller
  • 46,381
  • 14
  • 112
  • 137
0

diff -u <some args here> | grep '^[+-]'

Or you could just not produce the extra lines at all:

diff --unified=0 <some args>

Ether
  • 53,118
  • 13
  • 86
  • 159
0
cat your_diff_file | sed '/^[+-]/!D'
Joy Dutta
  • 3,416
  • 1
  • 19
  • 19
  • Agree for this particular case. I typically use cat when using a long chain of sed commands to incrementally filter out data. If I have too big a data file to begin with, I replace cat with head -100 and the remaining part stays the same. – Joy Dutta Nov 12 '09 at 22:42
0
egrep "^[+-]" difffile >outputfile

Instead of deleting everything that doesn't match you show only lines that match. :)

Muxecoid
  • 1,193
  • 1
  • 9
  • 22
0

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.

Arthur Frankel
  • 4,695
  • 6
  • 35
  • 56
0
%!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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985