shell scripting: sed putting unnecessary parenthesis

the following should match only alphabets and if they are found, should put parenthesis around them. I don't know why it puts them between numbers like below. The following result is in the bash script.

$ echo '13123213'|sed 's/[a-z]*/(&)/g'
()1()3()1()2()3()2()1()3()

Answers


This might work for you (GNU sed):

sed 's/[[:alpha:]]\+/(&)/g' file

or:

sed 's/[[:alpha:]]\{1,\}/(&)/g' file

or:

sed 's/[[:alpha:]][[:alpha:]]*/(&)/g' file

Here you go:

echo '13123213'|sed 's/[0-9]/(&)/g'
(1)(3)(1)(2)(3)(2)(1)(3)

You do try to match [a-z], but you do not have any letter, so use [0-9] for numbers

Or if you like to get the parentheses around letters:

echo '1312a32z13'|sed 's/[a-z]/(&)/g'
1312(a)32(z)13

This awk can be used.

echo '13123213'| awk '{gsub(/[0-9]/,"(&)")}1'
(1)(3)(1)(2)(3)(2)(1)(3)

echo '13123213'| awk '{for (i=1;i<=NF;i++) $i="("$i")"}1' FS="" OFS=""
(1)(3)(1)(2)(3)(2)(1)(3)

Need Your Help

Date range in python where result value must be in string format

python django python-2.7 django-1.4

i want date range between two dates. the result value will be like it should support for string operations or it should be in string.