Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: integer to string, split and join with delimeter

Tags:

bash

shell

sh

I'm newbie in 'sh'. Is it possible to:

#!/usr/bin/env ruby
i=123
i.to_s.split('').join('.') #=> "1.2.3"

I've tried something but didn't succeed:

i=123
IFS='' read -a array <<< "$i"
echo $array #=> "123"
like image 992
ted Avatar asked Nov 15 '25 00:11

ted


2 Answers

You could use fold and paste:

$ i=123
$ echo $i | fold -w1 | paste -sd.
1.2.3
$ i=1234567890
$ echo $i | fold -w1 | paste -sd.
1.2.3.4.5.6.7.8.9.0
like image 138
devnull Avatar answered Nov 17 '25 19:11

devnull


Try this:

sed -r 's/\B/./g' <<< "$i"

Your sed needs to be able to handle extended regular expressions (option -r) for this, though.

like image 28
Alfe Avatar answered Nov 17 '25 19:11

Alfe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!