Explode a number in bash

Multi tool use
How would I explode out a number in bash
With this value
'12684041234'
Required result
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
bash
add a comment |
How would I explode out a number in bash
With this value
'12684041234'
Required result
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
bash
add a comment |
How would I explode out a number in bash
With this value
'12684041234'
Required result
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
bash
How would I explode out a number in bash
With this value
'12684041234'
Required result
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
bash
bash
asked Dec 27 at 13:28
moimoi
374
374
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
I don't think that there's any built-in way to do what you want, but you can always use a loop:
n=12684041234
for (( i = ${#n}; i > 0; i-- )) do echo ${n:0:i}; done
This just loops from the length of the variable $n
down to 1 and prints substrings of $n
.
add a comment |
The %
can be used to remove prefix of a variable's value.
Give a try to this:
number=12684041234
while [[ "${#number}" -gt 0 ]] ; do
printf ",'%s'" "${number}"
number="${number%?}"
done | cut -b 2-
Output
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
You can also usewhile [[ -n $number ]]
to loop while the variable is not empty.
– glenn jackman
Dec 27 at 15:22
1
@glennjackman Or just[[ $number ]]
for that matter.
– mickp
Dec 27 at 15:28
add a comment |
Using GNU awk:
$ echo 12684041234 | awk 'BEGIN{FS=OFS=""}{for(i=NF;i>=1;i--){print;NF--}}'
12684041234
1268404123
126840412
...
add a comment |
if n is less than or equals echo $((2**63-1))
for((n=12684041234;n>0;n/=10));do echo $n;done
I like that loop. To match the output exactly, you could:echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
add a comment |
with dc :
echo '12684041234' | dc -f - -e '[lap10/dsa0<Z]sZsalZx'
add a comment |
sed
solution for fun:
$ sed -n ':a;p;s/.$//;/./ba' <<< 12684041234
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
add a comment |
Using Perl and regex
$ export a='12684041234'
$ echo $a | perl -ne ' $x=$_;$i=length($x); while($i>0) { $x=~m/(.{$i})/m; print "$1n" ; $i-- } '
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
Thanks to Nahuel for the below solution
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
orperl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation/
../
is a regex,.+
matches at least one character as many as possible,(?{
code})
allows to execute code after a partial match, to get submatches,(?!)
makes the match fail and leads to backtracking, the^
at beginning to anchor at the beginning,-E
to usesay
to shorten.
– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, noteexport
is useless here, alsoecho
+|
makes a subshell which can be avoided using here-string
– Nahuel Fouilleul
Dec 27 at 20:42
add a comment |
I ain't thinkin' good practices, but to solver this problem. Here's how in Python the solution would be:
from functools import reduce
def f_split_word(s_word):
return s_word
def f_list_explode_word(s_word):
ret =
s_word_split = list(map(f_split_word, s_word))
for x in range(0, len(s_word_split)):
ret.append(reduce(lambda x,y: str(x) + str(y), s_word_split if x == 0 else s_word_split[:-x]))
return ret
s_word = str('12684041234')
print(f_list_explode_word(s_word))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945896%2fexplode-a-number-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think that there's any built-in way to do what you want, but you can always use a loop:
n=12684041234
for (( i = ${#n}; i > 0; i-- )) do echo ${n:0:i}; done
This just loops from the length of the variable $n
down to 1 and prints substrings of $n
.
add a comment |
I don't think that there's any built-in way to do what you want, but you can always use a loop:
n=12684041234
for (( i = ${#n}; i > 0; i-- )) do echo ${n:0:i}; done
This just loops from the length of the variable $n
down to 1 and prints substrings of $n
.
add a comment |
I don't think that there's any built-in way to do what you want, but you can always use a loop:
n=12684041234
for (( i = ${#n}; i > 0; i-- )) do echo ${n:0:i}; done
This just loops from the length of the variable $n
down to 1 and prints substrings of $n
.
I don't think that there's any built-in way to do what you want, but you can always use a loop:
n=12684041234
for (( i = ${#n}; i > 0; i-- )) do echo ${n:0:i}; done
This just loops from the length of the variable $n
down to 1 and prints substrings of $n
.
answered Dec 27 at 13:37


Tom Fenech
54.1k65290
54.1k65290
add a comment |
add a comment |
The %
can be used to remove prefix of a variable's value.
Give a try to this:
number=12684041234
while [[ "${#number}" -gt 0 ]] ; do
printf ",'%s'" "${number}"
number="${number%?}"
done | cut -b 2-
Output
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
You can also usewhile [[ -n $number ]]
to loop while the variable is not empty.
– glenn jackman
Dec 27 at 15:22
1
@glennjackman Or just[[ $number ]]
for that matter.
– mickp
Dec 27 at 15:28
add a comment |
The %
can be used to remove prefix of a variable's value.
Give a try to this:
number=12684041234
while [[ "${#number}" -gt 0 ]] ; do
printf ",'%s'" "${number}"
number="${number%?}"
done | cut -b 2-
Output
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
You can also usewhile [[ -n $number ]]
to loop while the variable is not empty.
– glenn jackman
Dec 27 at 15:22
1
@glennjackman Or just[[ $number ]]
for that matter.
– mickp
Dec 27 at 15:28
add a comment |
The %
can be used to remove prefix of a variable's value.
Give a try to this:
number=12684041234
while [[ "${#number}" -gt 0 ]] ; do
printf ",'%s'" "${number}"
number="${number%?}"
done | cut -b 2-
Output
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
The %
can be used to remove prefix of a variable's value.
Give a try to this:
number=12684041234
while [[ "${#number}" -gt 0 ]] ; do
printf ",'%s'" "${number}"
number="${number%?}"
done | cut -b 2-
Output
'12684041234','1268404123','126840412','12684041','1268404','126840','12684','1268','126','12','1'
edited Dec 27 at 15:21
glenn jackman
165k26142234
165k26142234
answered Dec 27 at 13:39


Jay jargot
1,9191410
1,9191410
You can also usewhile [[ -n $number ]]
to loop while the variable is not empty.
– glenn jackman
Dec 27 at 15:22
1
@glennjackman Or just[[ $number ]]
for that matter.
– mickp
Dec 27 at 15:28
add a comment |
You can also usewhile [[ -n $number ]]
to loop while the variable is not empty.
– glenn jackman
Dec 27 at 15:22
1
@glennjackman Or just[[ $number ]]
for that matter.
– mickp
Dec 27 at 15:28
You can also use
while [[ -n $number ]]
to loop while the variable is not empty.– glenn jackman
Dec 27 at 15:22
You can also use
while [[ -n $number ]]
to loop while the variable is not empty.– glenn jackman
Dec 27 at 15:22
1
1
@glennjackman Or just
[[ $number ]]
for that matter.– mickp
Dec 27 at 15:28
@glennjackman Or just
[[ $number ]]
for that matter.– mickp
Dec 27 at 15:28
add a comment |
Using GNU awk:
$ echo 12684041234 | awk 'BEGIN{FS=OFS=""}{for(i=NF;i>=1;i--){print;NF--}}'
12684041234
1268404123
126840412
...
add a comment |
Using GNU awk:
$ echo 12684041234 | awk 'BEGIN{FS=OFS=""}{for(i=NF;i>=1;i--){print;NF--}}'
12684041234
1268404123
126840412
...
add a comment |
Using GNU awk:
$ echo 12684041234 | awk 'BEGIN{FS=OFS=""}{for(i=NF;i>=1;i--){print;NF--}}'
12684041234
1268404123
126840412
...
Using GNU awk:
$ echo 12684041234 | awk 'BEGIN{FS=OFS=""}{for(i=NF;i>=1;i--){print;NF--}}'
12684041234
1268404123
126840412
...
answered Dec 27 at 14:27


James Brown
18k31635
18k31635
add a comment |
add a comment |
if n is less than or equals echo $((2**63-1))
for((n=12684041234;n>0;n/=10));do echo $n;done
I like that loop. To match the output exactly, you could:echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
add a comment |
if n is less than or equals echo $((2**63-1))
for((n=12684041234;n>0;n/=10));do echo $n;done
I like that loop. To match the output exactly, you could:echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
add a comment |
if n is less than or equals echo $((2**63-1))
for((n=12684041234;n>0;n/=10));do echo $n;done
if n is less than or equals echo $((2**63-1))
for((n=12684041234;n>0;n/=10));do echo $n;done
answered Dec 27 at 14:36
Nahuel Fouilleul
14k11525
14k11525
I like that loop. To match the output exactly, you could:echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
add a comment |
I like that loop. To match the output exactly, you could:echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
I like that loop. To match the output exactly, you could:
echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
I like that loop. To match the output exactly, you could:
echo "'$n'"; done | paste -sd,
– glenn jackman
Dec 27 at 15:25
add a comment |
with dc :
echo '12684041234' | dc -f - -e '[lap10/dsa0<Z]sZsalZx'
add a comment |
with dc :
echo '12684041234' | dc -f - -e '[lap10/dsa0<Z]sZsalZx'
add a comment |
with dc :
echo '12684041234' | dc -f - -e '[lap10/dsa0<Z]sZsalZx'
with dc :
echo '12684041234' | dc -f - -e '[lap10/dsa0<Z]sZsalZx'
answered Dec 27 at 15:27
ctac_
1,787138
1,787138
add a comment |
add a comment |
sed
solution for fun:
$ sed -n ':a;p;s/.$//;/./ba' <<< 12684041234
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
add a comment |
sed
solution for fun:
$ sed -n ':a;p;s/.$//;/./ba' <<< 12684041234
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
add a comment |
sed
solution for fun:
$ sed -n ':a;p;s/.$//;/./ba' <<< 12684041234
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
sed
solution for fun:
$ sed -n ':a;p;s/.$//;/./ba' <<< 12684041234
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
answered Dec 27 at 15:41
mickp
39319
39319
add a comment |
add a comment |
Using Perl and regex
$ export a='12684041234'
$ echo $a | perl -ne ' $x=$_;$i=length($x); while($i>0) { $x=~m/(.{$i})/m; print "$1n" ; $i-- } '
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
Thanks to Nahuel for the below solution
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
orperl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation/
../
is a regex,.+
matches at least one character as many as possible,(?{
code})
allows to execute code after a partial match, to get submatches,(?!)
makes the match fail and leads to backtracking, the^
at beginning to anchor at the beginning,-E
to usesay
to shorten.
– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, noteexport
is useless here, alsoecho
+|
makes a subshell which can be avoided using here-string
– Nahuel Fouilleul
Dec 27 at 20:42
add a comment |
Using Perl and regex
$ export a='12684041234'
$ echo $a | perl -ne ' $x=$_;$i=length($x); while($i>0) { $x=~m/(.{$i})/m; print "$1n" ; $i-- } '
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
Thanks to Nahuel for the below solution
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
orperl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation/
../
is a regex,.+
matches at least one character as many as possible,(?{
code})
allows to execute code after a partial match, to get submatches,(?!)
makes the match fail and leads to backtracking, the^
at beginning to anchor at the beginning,-E
to usesay
to shorten.
– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, noteexport
is useless here, alsoecho
+|
makes a subshell which can be avoided using here-string
– Nahuel Fouilleul
Dec 27 at 20:42
add a comment |
Using Perl and regex
$ export a='12684041234'
$ echo $a | perl -ne ' $x=$_;$i=length($x); while($i>0) { $x=~m/(.{$i})/m; print "$1n" ; $i-- } '
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
Thanks to Nahuel for the below solution
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
Using Perl and regex
$ export a='12684041234'
$ echo $a | perl -ne ' $x=$_;$i=length($x); while($i>0) { $x=~m/(.{$i})/m; print "$1n" ; $i-- } '
12684041234
1268404123
126840412
12684041
1268404
126840
12684
1268
126
12
1
Thanks to Nahuel for the below solution
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
edited 2 days ago
answered Dec 27 at 14:09
stack0114106
2,0051416
2,0051416
orperl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation/
../
is a regex,.+
matches at least one character as many as possible,(?{
code})
allows to execute code after a partial match, to get submatches,(?!)
makes the match fail and leads to backtracking, the^
at beginning to anchor at the beginning,-E
to usesay
to shorten.
– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, noteexport
is useless here, alsoecho
+|
makes a subshell which can be avoided using here-string
– Nahuel Fouilleul
Dec 27 at 20:42
add a comment |
orperl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation/
../
is a regex,.+
matches at least one character as many as possible,(?{
code})
allows to execute code after a partial match, to get submatches,(?!)
makes the match fail and leads to backtracking, the^
at beginning to anchor at the beginning,-E
to usesay
to shorten.
– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, noteexport
is useless here, alsoecho
+|
makes a subshell which can be avoided using here-string
– Nahuel Fouilleul
Dec 27 at 20:42
or
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
or
perl -nE '/^.+(?{say$&})(?!)/' <<<12684041234
– Nahuel Fouilleul
Dec 27 at 15:46
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
@Nahuel.. yes it works..could you pls explain further.. I'll add to the answer
– stack0114106
Dec 27 at 18:00
just golfed a bit for fun. explanation
/
../
is a regex, .+
matches at least one character as many as possible, (?{
code})
allows to execute code after a partial match, to get submatches, (?!)
makes the match fail and leads to backtracking, the ^
at beginning to anchor at the beginning, -E
to use say
to shorten.– Nahuel Fouilleul
Dec 27 at 20:39
just golfed a bit for fun. explanation
/
../
is a regex, .+
matches at least one character as many as possible, (?{
code})
allows to execute code after a partial match, to get submatches, (?!)
makes the match fail and leads to backtracking, the ^
at beginning to anchor at the beginning, -E
to use say
to shorten.– Nahuel Fouilleul
Dec 27 at 20:39
about submatches removing anchor will give all substrings, note
export
is useless here, also echo
+ |
makes a subshell which can be avoided using here-string– Nahuel Fouilleul
Dec 27 at 20:42
about submatches removing anchor will give all substrings, note
export
is useless here, also echo
+ |
makes a subshell which can be avoided using here-string– Nahuel Fouilleul
Dec 27 at 20:42
add a comment |
I ain't thinkin' good practices, but to solver this problem. Here's how in Python the solution would be:
from functools import reduce
def f_split_word(s_word):
return s_word
def f_list_explode_word(s_word):
ret =
s_word_split = list(map(f_split_word, s_word))
for x in range(0, len(s_word_split)):
ret.append(reduce(lambda x,y: str(x) + str(y), s_word_split if x == 0 else s_word_split[:-x]))
return ret
s_word = str('12684041234')
print(f_list_explode_word(s_word))
add a comment |
I ain't thinkin' good practices, but to solver this problem. Here's how in Python the solution would be:
from functools import reduce
def f_split_word(s_word):
return s_word
def f_list_explode_word(s_word):
ret =
s_word_split = list(map(f_split_word, s_word))
for x in range(0, len(s_word_split)):
ret.append(reduce(lambda x,y: str(x) + str(y), s_word_split if x == 0 else s_word_split[:-x]))
return ret
s_word = str('12684041234')
print(f_list_explode_word(s_word))
add a comment |
I ain't thinkin' good practices, but to solver this problem. Here's how in Python the solution would be:
from functools import reduce
def f_split_word(s_word):
return s_word
def f_list_explode_word(s_word):
ret =
s_word_split = list(map(f_split_word, s_word))
for x in range(0, len(s_word_split)):
ret.append(reduce(lambda x,y: str(x) + str(y), s_word_split if x == 0 else s_word_split[:-x]))
return ret
s_word = str('12684041234')
print(f_list_explode_word(s_word))
I ain't thinkin' good practices, but to solver this problem. Here's how in Python the solution would be:
from functools import reduce
def f_split_word(s_word):
return s_word
def f_list_explode_word(s_word):
ret =
s_word_split = list(map(f_split_word, s_word))
for x in range(0, len(s_word_split)):
ret.append(reduce(lambda x,y: str(x) + str(y), s_word_split if x == 0 else s_word_split[:-x]))
return ret
s_word = str('12684041234')
print(f_list_explode_word(s_word))
answered Dec 27 at 14:44


Carlos Mesquita Aguiar
92
92
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53945896%2fexplode-a-number-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
G0 3QmsjPRjP9PtZp uTr qYS Sys