bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed problem with ^ and \


From: Bob Proulx
Subject: Re: sed problem with ^ and \
Date: Wed, 16 Mar 2005 10:06:45 -0700
User-agent: Mutt/1.5.6+20040907i

address@hidden wrote:
> I should probably have said that I was using sed -f and so my 
> search/replace filter was actually in a sedfile, hence no quotes. Sorry 
> about that.

What is really good is if you can reduce down to a test case that
anyone could run to see the behavior your are seeing.  Something like
my example, for example.

> What you didn't answer, or at least if you did I didn't understand the 
> answer, is actually where I was having my biggest problem.

> uteuds04:wtu_dev > sed --version
> GNU sed version 3.02

Yes, GNU sed version 3.02, thank you.

> ... doesn't recognise ^ to mean "start of line" or $ to mean "end of 
> line". I found a similar issue later with the pattern below (supposed to 
> trim spaces and nulls from the end of lines) not working because it 
> didn't understand the $. 
> 
>       s/ .*$//

That looks fine to me.  See the difference in the following output.

  echo 'foo   ' | od -c
  echo 'foo   ' | sed 's/ .*$//' | od -c

Note that in the context of '/.*$/' the $ is not needed.  The .* will
match as much as it can and will match all of the way to the end of
line.  So you don't need it there and I would not include it.

  echo 'foo   ' | sed 's/ .*//' | od -c

> Looking at the man page for regexp I found the following ...
> 
> ***
>        -lineanchor    Changes  the  behavior  of `^' and `$' (the
>                       ``anchors'') so they  match  the  beginning
>                       and  end  of  a line respectively.  This is
>                       the same as specifying  the  (?w)  embedded
>                       option (see METASYNTAX, below).
> ***
> 
> So my question is, can I get sed to interpret ^ to mean start of line 
> and $ to mean end of line and if so then how?

That is the default.  So I am at a loss as to the behavior you are
seeing.  In fact I would go so far as to say this is fundamental
behavior for sed and if that were broken then there would be many,
many reports of it.

Could you reduce the problem to something like this:

  echo 'foo' | sed 's/^foo/bar/'
  bar

Clearly that should how that ^ is matching the beginning of line.

> Regarding your gripe about my email disclaimer, all I can do is 
> apologise, stand corrected and thank you for the etiquette lesson. I 
> don't like them either but legal say we have to use them.

Please try to educate them.

> In order to suppress it I have to first remember and then jump
> through a couple of hoops hoping that no one is looking.

Thank you very much for the extra effort you put into the message.

If this is really such a problem for you then I would instead find a
free email account elsewhere on the Internet.  There are many free
email alternatives available.  Then you could use it for your
interactions instead of using your employer's account.

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]