grep help

219 views
Skip to first unread message

plib

unread,
Sep 15, 2008, 9:11:43 PM9/15/08
to TextWrangler Talk
Anyone wanna teach a noobie how to grep? I have a folder full of long
documents each containing a quoted text passage. How can I write a
grep search that will extract everything enclosed by quotes, and
delete everything else?

Tom Robinson

unread,
Sep 17, 2008, 10:07:30 AM9/17/08
to textwr...@googlegroups.com

You picked the wrong project to learn on!

For a start you'll need to get all your text into one document. 'cat'
at the command line could do this easily.

For the search the regular expression

".*?"

will find text within quotes. The quote matches itself, the dot
matches any single character, the asterisk says to match that single
character any number of times, the question mark says not to be
greedy--stop matching at the next quote rather than the last quote on
the line.

To delete everything which isn't in quotes you need to search for

(".*?")|[^\1]

and replace with

\1

You can see the original pattern buried in there, how the rest of it
works is still a mystery to me :-)

Note that in all the above every quote needs to have a mate--if you
have an extraneous or missing quote everything's going to turn to
custard.

Jim McCarty

unread,
Sep 17, 2008, 10:38:42 AM9/17/08
to textwr...@googlegroups.com

You should also read the chapter on grep in the TW User Manual --
accessible from the Help menu.

--

Calvin Trillin - "Anybody caught selling macrame in public should be
dyed a natural color and hung out to dry."

plib

unread,
Sep 17, 2008, 11:44:49 AM9/17/08
to TextWrangler Talk
You're right of course, Jim. I wouldn't have even known how to ask the
question without having referred to Help first. I spent over half a
day trying to figure it out myself before reaching out. But I'm like
you: whenever anyone asks me a software question (in my field I'm
often the Go-To Guy), I gently admonish them to try that little Help
menu staring them in the face 40+ hours a week. Thanks, guys!

On Sep 17, 7:38 am, "Jim McCarty" <hucknca...@gmail.com> wrote:

plib

unread,
Sep 17, 2008, 11:44:59 AM9/17/08
to TextWrangler Talk
WOW =oD

So elegantly simple, yet I never would've arrived at it though my
trial-and-error process. I had already gotten all my text into one doc
by running a multi-doc search for quotes.

Thanks 1,000,000 Tom! And equally or more so, thanks for the
explanation, as it also taught me how to make further edits I wasn't
going to burden these boards with.



On Sep 17, 7:07 am, Tom Robinson <barefootg...@tomrobinson.co.nz>
wrote:
Reply all
Reply to author
Forward
0 new messages