tags:

views:

282

answers:

5

Title sums it up.

$ echo `seq 0 10` `seq 5 15` | sort -n
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15

Why doesn't this work?

Even if I don't use seq:

echo '0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15' | sort -n
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15

And even ditching echo directly:

$ echo '0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15' > numbers
$ sort -n numbers 
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15
+9  A: 

sort(1) sorts lines. You have to parse whitespace delimited data yourself:

echo `seq 0 10` `seq 5 15` | tr " " "\n" | sort -n
Chris Lutz
Note that I can't test this with `seq(1)` (not installed on OS X), but this works for `echo(1)` for me just fine.
Chris Lutz
Duh... Thanks. *15 chars*
LiraNuna
You're welcome.
Chris Lutz
+5  A: 

You have single line of input. There is nothing to sort.

Kamil Szot
Well, technically, there *is* something to sort, but it's simple and blindingly fast :-)
paxdiablo
+8  A: 

Because you need newlines for sort:

$ echo `seq 0 10` `seq 5 15` | tr " " "\\n" | sort -n | tr "\\n" " "; echo ""
0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 10 11 12 13 14 15
$
Dirk Eddelbuettel
+3  A: 

The command as you typed it results in the sequence of numbers being all passed to sort in one line. That's not what you want. Just pass the output of seq directly to sort:

(seq 0 10; seq 5 15) | sort -n

By the way, as you just found out, the construct

echo `command`

doesn't usually do what you expect and is redundant for what you actually expect: It tells the shell to capture the output of command and pass it to echo, which produces it as output again. Just let the output of the command go through directly (unless you really mean to have it processed by echo, maybe to expand escape sequences, or to collapse everything to one line).

Idelic
'doesn't do what you expect' - Actually it did *exactly* what I expected it to do. I just didn't remember `sort` to sort lines :D
LiraNuna
I was referring to the general use of the construct. You usually don't expect to collapse all text into one line, for example, or the output of the command to have special characters interpreted (think of security issues, for example). Edited to add "usually", though.
Idelic
A: 

On Mac OS X you can mimic seq by using jot:

echo $(jot 15 0 15) $(jot 15 0 15)

By the way, to make sort sort space-delimited data you have to modify the sort source code (& compile it).

ya2cts