views:

216

answers:

6

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.

A: 

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

ennuikiller
A: 

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

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

diff --unified=0 <some args>

Ether
A: 
cat your_diff_file | sed '/^[+-]/!D'
Joy Dutta
useless use of `cat`: `sed` accepts filenames as arguments
Dennis Williamson
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
+8  A: 

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
Great, thanks! This is a good fix.
mager
if you want to save one keystroke: ':v' is a synonym to ':g!' :)
Leonardo Constantino
That saves two keystrokes! Shift, 1. Neat.
Marcin
A: 
egrep "^[+-]" difffile >outputfile

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

Muxecoid
You definitely need another caret and may not need "e" or quotes. This works for me: `grep ^[^+-]`
Dennis Williamson
A: 

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
Thanks!! This is awesome!!
mager