Re: Finding lines that do NOT contain a pattern

1,314 views
Skip to first unread message

Christopher Stone

unread,
Aug 27, 2012, 10:50:28 AM8/27/12
to bbe...@googlegroups.com
On Aug 27, 2012, at 00:47, gugus2000 <mar...@ruggiero.ch> wrote:
How can I remove lines from a large file that do not contain a certain pattern? I can easily find the lines with the pattern but then...? The way I do it today is using grep on the command line with io-redirection but I think there should be a way to do it from within BBEdit.
______________________________________________________________________

Hey Markus,

Currently there is no way to do that directly with BBEdit.

Please make a feature request to support: Bare Bones Software <sup...@barebones.com>

The more of us who do the more likely they'll listen.

Personally I think the inversion switch should be included in the Process Duplicate Lines dialog, but I made my formal request some time ago.

In the meantime it's pretty easy to roll-your-own:

------------------------------------------------------------------------------------------------
try

  

  tell application "BBEdit"
    tell me to set ddStr to text returned of (display dialog "Enter grep pattern:" default answer "" giving up after 120)
    if ddStr"" then
      set _text to quoted form of (get text of front text window)
      set shellCMD to "echo " & _text & " | tr '\\r' '\\n' | egrep -iv " & "\"" & ddStr & "\""
      set filteredText to do shell script shellCMD
      make new text document with properties {text:filteredText}
    end if
  end tell

  

on error eMsg number eNum
  set {c, s} to {return, "------------------------------------------"}
  set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & s
  beep
  set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then
    set the clipboard to e
  end if
end try
------------------------------------------------------------------------------------------------

I've got case-sensitive set OFF in the script.

--
Best Regards,
Chris

Maarten Sneep

unread,
Aug 27, 2012, 4:08:41 PM8/27/12
to bbe...@googlegroups.com

On 27 aug. 2012, at 07:47, gugus2000 <mar...@ruggiero.ch> wrote:

> How can I remove lines from a large file that do not contain a certain pattern? I can easily find the lines with the pattern but then...? The way I do it today is using grep on the command line with io-redirection but I think there should be a way to do it from within BBEdit.

Process lines containing, and copy the lines that do contain the certain pattern into a new file, or use the clipboard as an intermediate.

Best,

Maarten

Tom Robinson

unread,
Aug 27, 2012, 8:58:37 PM8/27/12
to bbe...@googlegroups.com
This GREP pattern can be used to delete lines which don't begin with 'save', but I don't know enough about negative lookahead assertions to know if it can be changed to look anywhere in the line:

^(?!save)


On 2012-08-27, at 17:47, gugus2000 <mar...@ruggiero.ch> wrote:

> How can I remove lines from a large file that do not contain a certain pattern? I can easily find the lines with the pattern but then...? The way I do it today is using grep on the command line with io-redirection but I think there should be a way to do it from within BBEdit.
>
> Thanks for your help
> ---markus---

Olof Svensson

unread,
Aug 28, 2012, 3:02:37 AM8/28/12
to bbe...@googlegroups.com, gugus2000
English is not my first language. I try to keep it short but I don't want to sound unfriendly.

If you have a file (MyFile):

George Smily, Modesty Blaise, Olof
Karla, Willy Garvin, Zorro
George Smily, Modesty Blaise, Karla
Olof, Karla, Willy Garvin, Zorro

Try a shellscript like:

egrep -v 'Olof' Path-to-MyFile | bbedit

This will exclude all lines containing 'Olof' and you will have:

Karla Willy Garvin Zorro
George Smily, Modesty Blaise, Karla

in a new document in BBedit.

--
Olof Svensson

--------------------
2 rules to success in life.
1. Don't tell people everything you know.
Reply all
Reply to author
Forward
0 new messages