[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Simple Grep Command?
From: |
Dave B |
Subject: |
Re: Simple Grep Command? |
Date: |
Thu, 17 Sep 2009 09:29:09 +0100 |
User-agent: |
KMail/1.11.2 (Linux/2.6.28-15-generic; KDE/4.2.2; x86_64; ; ) |
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.