Simple Grep Command?

View: New views
4 Messages — Rating Filter:   Alert me  

Simple Grep Command?

by Newbie407 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm new to Grep.  I'm using Text Wrangler.

I want to search an entire doc for a pattern.  If I find that pattern, I want to do a search and replace within the pattern, then keep looking for the pattern again.

Specifically, I'm looking for the letters "LEC" followed by variable text and ending with either a carriage return or the letters "ulty."  If I find it, I want to change all the space characters within the found string into something else, say "zzzz."  

I can figure out how to find the pattern, and I can figure out how to make the replacement, but I can't figure out how to do it within the pattern only.

Thanks!

Re: Simple Grep Command?

by Dave B-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 16 September 2009 21:49:14 Newbie407 wrote:

> I'm new to Grep.  I'm using Text Wrangler.
>
> I want to search an entire doc for a pattern.  If I find that pattern, I
> want to do a search and replace within the pattern, then keep looking for
> the pattern again.
>
> Specifically, I'm looking for the letters "LEC" followed by variable text
> and ending with either a carriage return or the letters "ulty."  If I find
> it, I want to change all the space characters within the found string into
> something else, say "zzzz."
>
> I can figure out how to find the pattern, and I can figure out how to make
> the replacement, but I can't figure out how to do it within the pattern
> only.

Grep can't do replacements. To do what you want, you need a tool like sed, awk
or perl. Although you did not specify what you mean by "variable text"
following "LEC" (or at least it's an ambiguous definition), I think the
following perl code might do:

perl -ne 'print if s/(LEC.*?(?:ulty|$))/($a=$1)=~s,\s,zzzz,g;$a/ge'

Note that due to the ambiguous problem statement you provided, the above code
might also turn out to be wrong for you.




Re: Simple Grep Command?

by Newbie407 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dave,

Many thanks for your help.

Two items. First, when I say "variable text" I just mean that there can be text of any length between the "LEC" and the carriage return.

But I'm confused when you say that Grep can't do replacements.  I'm using this in the context of TextWrangler, and it's all about finding and replacing text.  Maybe my newness is standing out here. . . .

I will look up info on PERL and see if I can make your suggestion work.


Grep can't do replacements. To do what you want, you need a tool like sed, awk
or perl. Although you did not specify what you mean by "variable text"
following "LEC" (or at least it's an ambiguous definition), I think the
following perl code might do:

perl -ne 'print if s/(LEC.*?(?:ulty|$))/($a=$1)=~s,\s,zzzz,g;$a/ge'

Note that due to the ambiguous problem statement you provided, the above code
might also turn out to be wrong for you.






Re: Simple Grep Command?

by Dave B-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thursday 17 September 2009 17:00:37 Newbie407 wrote:

> Two items. First, when I say "variable text" I just mean that there can be
> text of any length between the "LEC" and the carriage return.

Ok, but you don't say how, for example, the following should be considered:

aaa LEC foo bar baz ulty abc def LEC yyy xxx ulty

is that two instances of your pattern, or a single one, or it can never
happen?

> But I'm confused when you say that Grep can't do replacements.  I'm using
> this in the context of TextWrangler, and it's all about finding and
> replacing text.  Maybe my newness is standing out here. . . .

I don't know what textwrangler is, but grep itself is a tool that only does
pattern matching, ie print lines matching a pattern. No text replacement is
possible with grep alone.

--
D.