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

Re: grep replacement using sed is behaving oddly



On Fri, Oct 21, 2022 at 08:01:00PM +0200, tomas@tuxteam.de wrote:
> On Fri, Oct 21, 2022 at 01:21:44PM -0400, Gary Dale wrote:
> > I'm hoping someone can tell me what I'm doing wrong. I have a line in a lot
> > of HTML files that I'd like to remove. The line is:
> > 
> >             <hr  style="border-top: 1px solid rgb(0, 32, 159); margin:
> > 0rem;">
> > 
> > I'm testing the sed command to remove it on just one file. When it works,
> > I'll run it against *.html. My command is:
> > 
> >  sed -i -s 's/\s*\<hr\ \ style.*\>//g' history.html
> > 
> > Unfortunately, the replacement doesn't remove the line but rather leaves me
> > with:
> > 
> >             <;">
> 
> This looks as if the <> in the regexp were interpreted as left and right
> word boundaries (but that would only be the case if you'd given the -E
> (or -r) option).
> 
> Try explicitly adding the --posix option, perhaps...

Gary is using non-POSIX syntax (specifically the \s), so that's not going
to help unless he first changes his regular expression to be standard.

I think you might be on to something with the \< and \> here.  I can see
absolutely no reason why Gary put backslashes in front of spaces and
angle brackets here.  The backslashes in front of the spaces are probably
just noise, and can be ignored.  The \< and \> on the other hand might
be interpreted as something special, the same way \s is (because this is
GNU sed, which loves to do nonstandard things).

unicorn:~$ echo 'abc <foo> xyz' | sed 's/<.*>//'
abc  xyz
unicorn:~$ echo 'abc <foo> xyz' | sed 's/\<.*\>//'

unicorn:~$ 

So... yeah, \< and/or \> clearly have some special meaning to GNU sed.
Good luck figuring out what that is.

For Gary's actual problem, simply removing the backslashes where they're
not wanted would be a good start.  Actually learning sed could be step 2.

I feel obliged at this point to mention that parsing HTML with regular
expressions is a fool's errand, and that sed should not be the tool of
choice here.  Nor should grep, nor any other RE-based tool.  This goes
triple when one doesn't even know the correct syntax for their RE.

https://stackoverflow.com/q/1732348


Reply to: