Reputation: 291
for example if we have in a variable named "var"
a string "2.test 1.test 9.test"
i want it to be
1.test 2.test 9.test
I was trying to apply this command
echo $var | sort -n
but the output isn't correct because if for example I have
2.text 11.text
it will print
11.text 2.text which is wrong because 11>2
thanks
Upvotes: 1
Views: 1228
Reputation: 11
convert white spaces in linebreaks with tr
; and now sort
; and convert linebreaks back to white spaces
echo "2.test 1.test 9.test" | tr " " "\n" | sort | tr "\n" " "
ps. saw this somewhere in a forum
Upvotes: 1
Reputation: 263237
sort
works on lines, not words.
For the example you've shown us, you're sorting a single line of text. For example:
$ echo 2.text 11.text 3.text | sort -n
2.text 11.text 3.text
But that's inconsistent with the output you've shown us, so I can't be sure just what you're doing, or what you're trying to do.
Are you looking for something like this?
$ echo 2.text 11.text 3.text | fmt -1
2.text
11.text
3.text
$ echo 2.text 11.text 3.text | fmt -1 | sort -n
2.text
3.text
11.text
And do you need to re-assemble the lines into a single line? Piping the output through fmt -999
will do that, but that's a bit ugly (GNU coreutils fmt
limits the width to 2500).
Upvotes: 1