Question: Inserting delim between numbers and strings in bash
0
gravatar for mbk0asis
10 months ago by
mbk0asis240
Korea, Republic Of
mbk0asis240 wrote:

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 • 356 views
ADD COMMENTlink modified 10 months ago by j_susat40 • written 10 months ago by mbk0asis240
3

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

ADD REPLYlink written 10 months ago by WouterDeCoster22k
5
gravatar for shenwei356
10 months ago by
shenwei3563.4k
China
shenwei3563.4k wrote:

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'
ADD COMMENTlink modified 10 months ago • written 10 months ago by shenwei3563.4k

Thank you. It works perfectly!

ADD REPLYlink written 10 months ago by mbk0asis240
1
gravatar for zjhzwang
10 months ago by
zjhzwang180
zjhzwang180 wrote:

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
ADD COMMENTlink written 10 months ago by zjhzwang180
1
gravatar for j_susat
10 months ago by
j_susat40
Kiel
j_susat40 wrote:

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.

ADD COMMENTlink written 10 months ago by j_susat40
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 759 users visited in the last hour