0

I have a text file open in BBEdit/InDesign with email addresses on some lines (about a third of the lines) and name and date stuff on the other lines. I just want to keep the lines that have emails and remove all the others.

A simple pattern I can see to eliminate all the lines apart from those with email addresses on them is to have a negative match for the @ character.

I can't use grep -v pattern because the Find and Replace implementation of grep dialogue box just has the fields for Find pattern and Replace pattern. grep -something options don't exist in this context.

Note, I am note trying to construct a valid email address test at all, just using the presence of one (or more) @ character to allow a line to stay, all other lines must be deleted from the list.

The closest I got was a pattern which hits only the email address lines (opposite outcome of my goal):

^((\w+|[ \.])\w+)[?@].*$

I tried various combination of ^.*[^@].*$ and more sophisticated /w and [/w|\.] in parentheses and escaping the @ with [^\@] and negative look forwards like (!?).

I want to find these non-email address lines and delete them using any of these apps on OS X BBEdit/InDesign. I will use the command line if I have to. There must be a way using in-app Find and Replace with grep though I'd expect.

wide_eyed_pupil
  • 3,153
  • 7
  • 24
  • 35
  • 1
    `grep -v @ file` – tripleee May 09 '18 at 17:22
  • Regular `grep` doesn't understand `\w` though you might have `grep -P` or some other nonstandard version. – tripleee May 09 '18 at 17:23
  • 1
    Possible duplicate of [Remove all lines except matching pattern line best practice (sed)](https://stackoverflow.com/questions/5922706/remove-all-lines-except-matching-pattern-line-best-practice-sed) – Benjamin W. May 09 '18 at 17:25
  • 1
    I don't quite understand why you want to grep the non-matching lines, but in the end you actually want to keep the matching lines, which is `grep '@' infile`. – Benjamin W. May 09 '18 at 17:26
  • Agree with @BenjaminW. - looking for the lines to delete (and then deleting them) is way more complicated than just keeping the lines you want. – Mike Harris May 09 '18 at 19:11
  • I'm actually looking to use the `grep` functions within text processing apps like BBEdit, InDesign etc. So optionals like -v cannot be used to best of my knowledge. – wide_eyed_pupil May 10 '18 at 09:38
  • BBEdit has a “Process lines containing...” option which would seem ideal here. – steveax May 15 '18 at 05:00
  • Try [this pattern](https://regex101.com/r/AhZwWI/1/). It works successfully in InDesign using _Grep Find/Change_, however I don't have _BBEdit_ to test it (should be ok though as it works successfully with _Sublime Text_). Just leave the _change to_ value ( i.e. replacement value) empty. This will delete the complete line including line break. If your preference is to keep an empty line space where the line is removed just make it less greedy (i.e. Add a `?` to the end of the whole pattern). – RobC May 17 '18 at 16:02

1 Answers1

0

As stated in the comments grep -v @ filename lists all lines without an @ symbol. You could also use grep @ filename > new_filename The file new_filename will consist only of lines with @. You can use this new file or delete all lines in the old file and paste contents of new file into it.

Natsfan
  • 4,093
  • 3
  • 22
  • 29
  • Thanks, I was hoping to do it within BBEdit or InDesign but happy to do it on a file from command line if I have to. `grep` function in apps like InDesign doesn't allow options like the command line `grep` – wide_eyed_pupil May 10 '18 at 09:40
  • I'm going to edit Question to make it clearer that I want to know how to do in text handling apps that deploy a `grep` conforming find and replace query engine. – wide_eyed_pupil May 10 '18 at 13:14