This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Inserting delim between numbers and strings in bash

Hi.

How can I insert a delimiter between number and string?

I tried using 'sed' but couldn't get the result I want.

Here's an example and the code I used.

ex) 5c  --> 5,c   

sed 's/[0-9][a-zA-Z]/[0-9],[a-zA-Z]/g'

Thank you!

bash

"A programmer has a problem and thinks 'I know, I'll use regular expressions'. Now he has two problems."

3 answers

sed version

sed -r 's/([0-9]+)([a-zA-Z]+)/\1,\2/g'

Perl version

perl -pe 's/([0-9]+)([a-zA-Z]+)/\1,\2/g'

or

perl -pe 's/([0-9]+)([a-zA-Z]+)/$1,$2/g'

Thank you. It works perfectly!

In sed .\d and \w might not work,or maybe I don't know how to use.Try this:

sed  -e 's/\([0-9]\+\)\([a-zA-Z]\+\)/\1,\2/g'  FILE > OUTPUT

How about this one:

sed -r 's/([0-9])/&,/g' FILE > OUTPUT

it's basically the same as above but without the picket fence in the end.

Log in to answer this question.