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

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

ADD REPLYlink written 3.9 years ago by WouterDeCoster44k
5
gravatar for shenwei356
3.9 years ago by
shenwei3565.5k
China
shenwei3565.5k 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 3.9 years ago • written 3.9 years ago by shenwei3565.5k

Thank you. It works perfectly!

ADD REPLYlink written 3.9 years ago by mbk0asis570
1
gravatar for zjhzwang
3.9 years 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 3.9 years ago by zjhzwang180
1
gravatar for j_susat
3.9 years 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 3.9 years 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: 1010 users visited in the last hour