why am I getting spaces in sed 's/[a-z]*/(&)/g'

后端 未结 3 1610
甜味超标
甜味超标 2021-01-16 04:28

I want to add parenthesis to all words I used

sed \'s/[a-z]*/(&)/g\' 

inputfile.txt

hola crayola123456
abc123456
         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 04:59

    The reason is that you are using a regex that can match an empty string. [a-z]* can match any empty space before a char since regex "sees" (i.e. checks) these positions. You need to replace the * (matching zero or more occurrences) with + quantifier (to match one or more characters).

    Here is an example of how this can be implemented in GNU sed:

    echo "hola crayola123456" | sed 's/[a-z]\+/(&)/g' 
    

    See the online demo

    On Mac, as per anubhava's comment, you need to use E option and use an unescaped +:

    echo "hola crayola123456" | sed -E 's/[a-z]+/(&)/g'
    

提交回复
热议问题