How do I sort an array with letters and numbers combined in bash? [duplicate]
This question already has an answer here:
How to create a function that can sort an array in bash?
4 answers
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
marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to create a function that can sort an array in bash?
4 answers
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
marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56
add a comment |
This question already has an answer here:
How to create a function that can sort an array in bash?
4 answers
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
This question already has an answer here:
How to create a function that can sort an array in bash?
4 answers
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
.
This question already has an answer here:
How to create a function that can sort an array in bash?
4 answers
bash sort array
bash sort array
edited Dec 27 '18 at 13:20
Mercyfon
asked Dec 26 '18 at 14:35
MercyfonMercyfon
205
205
marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by roaima, Jeff Schaller, Volker Siegel, RalfFriedl, msp9011 Dec 27 '18 at 6:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56
add a comment |
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56
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
Dec 26 '18 at 22:22
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 |
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 Dec 26 '18 at 14:49
msp9011msp9011
3,82843863
3,82843863
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
Dec 26 '18 at 22:22
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
Dec 26 '18 at 22:22
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 Dec 26 '18 at 17:50
RudiCRudiC
4,2191312
4,2191312
This would be better if echo was substituted for printf
– D. Ben Knoble
Dec 26 '18 at 22:22
add a comment |
This would be better if echo was substituted for printf
– D. Ben Knoble
Dec 26 '18 at 22:22
This would be better if echo was substituted for printf
– D. Ben Knoble
Dec 26 '18 at 22:22
This would be better if echo was substituted for printf
– D. Ben Knoble
Dec 26 '18 at 22:22
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 Dec 26 '18 at 14:53
answered Dec 26 '18 at 14:46
Ritajit KunduRitajit Kundu
857
857
add a comment |
add a comment |
That's a string; do you have that, exactly, or an actual array?
– Jeff Schaller
Dec 26 '18 at 14:42
i have that in an array, for example: ${array[0]} = h4, ${array[1]} = h5, ${array[2]} = h1, ${array[3]} = h2, ${array[4]} = h3
– Mercyfon
Dec 26 '18 at 14:56