Question: Inserting delim between numbers and strings in bash
0
gravatar for mbk0asis
8 months ago by
mbk0asis220
Korea, Republic Of
mbk0asis220 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 • 329 views
ADD COMMENTlink modified 8 months ago by j_susat40 • written 8 months ago by mbk0asis220
3

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

ADD REPLYlink written 8 months ago by WouterDeCoster20k
5
gravatar for shenwei356
8 months ago by
shenwei3563.2k
China
shenwei3563.2k 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 8 months ago • written 8 months ago by shenwei3563.2k

Thank you. It works perfectly!

ADD REPLYlink written 8 months ago by mbk0asis220
1
gravatar for zjhzwang
8 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 8 months ago by zjhzwang180
1
gravatar for j_susat
8 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 8 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: 1457 users visited in the last hour