Inserting delim between numbers and strings in bash
3
0
Entering edit mode
7.3 years ago
mbk0asis ▴ 680

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 • 1.7k views
ADD COMMENT
3
Entering edit mode

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

ADD REPLY
5
Entering edit mode
7.3 years ago

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 COMMENT
0
Entering edit mode

Thank you. It works perfectly!

ADD REPLY
1
Entering edit mode
7.3 years ago
zjhzwang ▴ 180

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 COMMENT
1
Entering edit mode
7.3 years ago
j_susat ▴ 40

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 COMMENT

Login before adding your answer.

Traffic: 2384 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6