How to search and replace

Paul Howarth paul at city-fan.org
Fri Feb 3 10:15:05 UTC 2006


Dan Track wrote:
> Just wondering if you could exaplin something to me:
> 
> 1) What does the following mean in plain english ^\(Target\[[^]]*\].
> I'm getting confused by the number of "[" brackets. To be specific I
> can't see how this part \[[^]]*\] is working. The \[ indicates the
> match to the "[" string but what I read from [^]] here is that I want
> the regular expression to match any of these ^] charaters. But there
> isn't a ^ charater or a ].

I'll take it element by element.

^ = Anchor the expression to the start of a line,

\( ... \) = sed "remembers" whatever matches the expression between \( 
and \) and will use that text in the replacement part of the 
search-and-replace - a "\1" means the first "remembered" text, a "\2" 
means the second "remembered" text etc.

Terget = boilerplate text to be matched

\[ = match the literal character "["

[^]] = match any character except "]"

> 2) What does $!N mean.

I pulled this from the sed FAQ, which explains it better than I could:

http://www.student.northpark.edu/pemente/sed/sedfaq4.html#s4.23.2

   Note that the 'N' followed by the 'P;D;' commands forms a "sliding
   window" technique. A window of N lines is formed. If the multi-line
   pattern matches, the block is handled. If not, the top line is printed
   and then deleted from the pattern space, and we try to match at the
   next line."

$!N literally means "if we're not at the end of the file, read the next 
line into the pattern space"

Paul.




More information about the fedora-list mailing list