简体   繁体   中英

OSX sed regex removal

I am trying to remove a string from a grep search, and I can't seem to get sed to work, despite the regex appearing to be correct. I am also using OSX, but I installed gnu-sed to make things a little easier.

I was hoping the following regex would remove all min certainties from :

gsed -r 's/minCertainty=\"\d\.\d{1,}\"//g'

Here is an example of the text I am dealing with, for context:

<vendor="company1" minCertainty="1.0"/>
<vendor="company2" minCertainty="0.75"/>
<vendor="company3" minCertainty="0.25"/>

\\d for digits is a perl extension to the standard regex syntax (either the basic or extended version). gsed -r uses the extended regex syntax, and doesn't include perl extensions. So use [0-9] or [[:digit:]] instead. Also, you don't need to escape the single-quotes in the expression (it doesn't seem to hurt, but I'd recommend against it). So use one of these:

gsed -r 's/minCertainty="[0-9]\.[0-9]{1,}"//g'
gsed -r 's/minCertainty="[[:digit:]]\.[[:digit:]]{1,}"//g'

On my machine, gsed doesn't seem to support \\d to detect digits—at least, it's not working here when I try it. The following regex works:

gsed -r 's/minCertainty=\\"[0-9]\\.[0-9]{1,}\\"//g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM