1

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

1
  • Should work, it works for me. Have you tried to reverse the order with -r? Commented Oct 19, 2011 at 21:51

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

I think paste -s should reconstruct the single line afterwards
@Daenyth: Yes, that should work. Add -d ' ' to delimit with spaces rather than tabs.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.