How do I sort an array in bash?
I have an array with h4 h5 h1 h2 h3
in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?
edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3
.
bash sort array
add a comment |
I have an array with h4 h5 h1 h2 h3
in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?
edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3
.
bash sort array
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
1
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago
add a comment |
I have an array with h4 h5 h1 h2 h3
in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?
edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3
.
bash sort array
I have an array with h4 h5 h1 h2 h3
in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this?
edit1: I would also like to sort an array via the numbers containing different letters, for example s4 h5 q1 h2 g3
.
bash sort array
bash sort array
edited 13 hours ago
Jeff Schaller
38.5k1053125
38.5k1053125
asked 14 hours ago
Mercyfon
184
184
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
1
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago
add a comment |
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
1
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
1
1
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago
add a comment |
3 Answers
3
active
oldest
votes
Try this,
Just print, sort and store the values in the same array name.
ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)
echo ${ary[@]}
h1 h2 h3 h4 h5
add a comment |
No need to use tr
; shell's "Parameter Expansion" with an adequate IFS
(in a subshell) should suffice. Try
$ ARR=(h4 h5 h1 h2 h3)
$ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
$ BRR=(s4 h5 q1 h2 g3)
$ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
$ echo "${SB[*]}"
q1 h2 g3 s4 h5
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
add a comment |
Lets take an array A
as
A=(h4 h5 h1 h2 h3)
Now, the problem with the sort
command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort
and put them in an array which is actually sorted, that is,
B=(`echo ${A[@]} | tr " " "n" | sort`)
Now, B is the sorted array. Here, tr
transforms space into a newline
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f490992%2fhow-do-i-sort-an-array-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this,
Just print, sort and store the values in the same array name.
ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)
echo ${ary[@]}
h1 h2 h3 h4 h5
add a comment |
Try this,
Just print, sort and store the values in the same array name.
ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)
echo ${ary[@]}
h1 h2 h3 h4 h5
add a comment |
Try this,
Just print, sort and store the values in the same array name.
ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)
echo ${ary[@]}
h1 h2 h3 h4 h5
Try this,
Just print, sort and store the values in the same array name.
ary=(h4 h5 h1 h2 h3)
ary=(`printf '%sn' "${ary[@]}"|sort`)
echo ${ary[@]}
h1 h2 h3 h4 h5
answered 14 hours ago
msp9011
3,71743863
3,71743863
add a comment |
add a comment |
No need to use tr
; shell's "Parameter Expansion" with an adequate IFS
(in a subshell) should suffice. Try
$ ARR=(h4 h5 h1 h2 h3)
$ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
$ BRR=(s4 h5 q1 h2 g3)
$ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
$ echo "${SB[*]}"
q1 h2 g3 s4 h5
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
add a comment |
No need to use tr
; shell's "Parameter Expansion" with an adequate IFS
(in a subshell) should suffice. Try
$ ARR=(h4 h5 h1 h2 h3)
$ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
$ BRR=(s4 h5 q1 h2 g3)
$ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
$ echo "${SB[*]}"
q1 h2 g3 s4 h5
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
add a comment |
No need to use tr
; shell's "Parameter Expansion" with an adequate IFS
(in a subshell) should suffice. Try
$ ARR=(h4 h5 h1 h2 h3)
$ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
$ BRR=(s4 h5 q1 h2 g3)
$ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
$ echo "${SB[*]}"
q1 h2 g3 s4 h5
No need to use tr
; shell's "Parameter Expansion" with an adequate IFS
(in a subshell) should suffice. Try
$ ARR=(h4 h5 h1 h2 h3)
$ SA=( $(IFS=$'n'; echo "${ARR[*]}" | sort) )
$ BRR=(s4 h5 q1 h2 g3)
$ SB=( $(IFS=$'n'; echo "${BRR[*]}" | sort -k1.2) )
$ echo "${SB[*]}"
q1 h2 g3 s4 h5
answered 11 hours ago
RudiC
4,1491312
4,1491312
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
add a comment |
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
This would be better if echo was substituted for printf
– D. Ben Knoble
6 hours ago
add a comment |
Lets take an array A
as
A=(h4 h5 h1 h2 h3)
Now, the problem with the sort
command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort
and put them in an array which is actually sorted, that is,
B=(`echo ${A[@]} | tr " " "n" | sort`)
Now, B is the sorted array. Here, tr
transforms space into a newline
add a comment |
Lets take an array A
as
A=(h4 h5 h1 h2 h3)
Now, the problem with the sort
command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort
and put them in an array which is actually sorted, that is,
B=(`echo ${A[@]} | tr " " "n" | sort`)
Now, B is the sorted array. Here, tr
transforms space into a newline
add a comment |
Lets take an array A
as
A=(h4 h5 h1 h2 h3)
Now, the problem with the sort
command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort
and put them in an array which is actually sorted, that is,
B=(`echo ${A[@]} | tr " " "n" | sort`)
Now, B is the sorted array. Here, tr
transforms space into a newline
Lets take an array A
as
A=(h4 h5 h1 h2 h3)
Now, the problem with the sort
command is that it sorts elements in different line and can't sort elements in the same line. So, the workaround is to transform the array into an element per line and sort with sort
and put them in an array which is actually sorted, that is,
B=(`echo ${A[@]} | tr " " "n" | sort`)
Now, B is the sorted array. Here, tr
transforms space into a newline
edited 14 hours ago
answered 14 hours ago
Ritajit Kundu
557
557
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f490992%2fhow-do-i-sort-an-array-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
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
14 hours ago
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
14 hours ago
1
Possible duplicate of How to create a function that can sort an array in bash?
– roaima
8 hours ago